4 ms·
What 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
by khyryk 4y ago
What 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.