4 ms·
I wrecked my brain last week trying to get generics to work correctly and i think Go just won’t support this. have a function take in an array of T T is gener
by wara23arish 4y ago
I wrecked my brain last week trying to get generics to work correctly and i think Go just won’t support this.
have a function take in an array of T
T is generic with type constraint call Constraint
problem is calling that function wont work because it has to instantiated with one of the types found in the Constraint which beats the whole purpose. I need this to pass in structs that belong to Constraint.
I tried a multitude of different things and nothing seemed to work.
I ended up reading the Go library for how it handles SPrintF args and did something similar with a type switch and reflection wizardry.
Im still a junior programmer so it could be that I just didnt know how or I underestimate the solution for this.
But I figured once we have a type Constraint defined with a function, it should be relatively simple to not have to instantiate it with ONE concrete type and allow the Constraint types as args.
if anyone knows a better way to do this please let me know
- markoman 4y agoI think its best to paste some code, i.e, the code (theoretically) meeting your requirements that failed, and code that compiled and got the closest to meeting your requirements. Hopefully someone can bridge that gap for you.
- wara23arish 4y agoI made an example of what Im trying to do. https://goplay.tools/snippet/Vj7uVMi0mWn https://goplay.tools/snippet/Vj7uVMi0mWn Basically have two different types that implement an interface, pass them into a receiver function in a composite type (array,slice). Part of the issue is that you cant have the receiver function without defining instantiating the type on the receiver Spotter in this case.
- markoman 4y agoYou don't actually have to instantiate the type for a receiver function (i.e., method). There's too much missing from your code for me to understand which direction you're going but you'll likely have to use a constraint for structs which is explained here: https://github.com/akutz/go-generics-the-hard-way/blob/main/04-getting-going/04-structural-constraints.md https://github.com/akutz/go-generics-the-hard-way/blob/main/...
- khyryk 4y agoWhat I'm getting from that is that you want some func that takes in a []T where the T is constrained to some set of types, and you want to call the func without having to specify the type that you're passing in using square brackets every time. If that's correct, then is this what you need? https://go.dev/play/p/_e-Uipgo5OQ https://go.dev/play/p/_e-Uipgo5OQ It seems quite straightforward so I think I'm missing some context, but this can be a start.
- wara23arish 4y agoThanks for your reply. I made an example of what Im trying to do. One thing I didnt specify is the function used is a receiver function. For some reason this means the type had to be instantiated on the receiver itself Spotter in this case. https://goplay.tools/snippet/Vj7uVMi0mWn https://goplay.tools/snippet/Vj7uVMi0mWn Basically have two different types that implement an interface, pass them into a receiver function in a composite type (array,slice). Part of the issue is that you cant have the receiver function without defining instantiating the type on the receiver Spotter in this case.
- khyryk 4y agoIf I'm, again, understanding what's happening, as the example above doesn't compile in its current state, then you'll probably run into this limitation: https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#No-parameterized-methods https://go.googlesource.com/proposal/+/refs/heads/master/des... I believe the most simple fix would be to have a separate function which is generic that takes in the struct along with the slice of T with the constraints that apply to it.