@Axel Wagner — 

Thank you for adding clarity to this.

Yes, it would be nice if we could get this to be handled with better type 
inference. 

But unless and until then, we have our workarounds.

On Saturday, October 21, 2023 at 4:01:05 PM UTC-4 Axel Wagner wrote:

> This is purely a type-inference problem. If you explicitly instantiate 
> `Append`, it works: https://goplay.tools/snippet/15RFAPFPl3y
> Type inference is still relatively limited. It might become possible to at 
> some point omit the instantiation. I think this might be "Inferring based 
> on interfaces" in https://github.com/golang/go/issues/58650
> Until then, it's expected that there will be some cases where you need to 
> specify the types.
>
> On Sat, Oct 21, 2023 at 9:45 PM Mike Schinkel <mi...@newclarity.net> 
> wrote:
>
>> No, that pre-generics case is not sufficient for my use-case.  
>>
>> For my use-case I need to be able to use other types besides []Suiter such 
>> as []int, e.g.:
>>
>> var n []int
>> n = Append(n, 1)
>> n = Append(n, 2)
>> n = Append(n, 3)
>> n = Append(n, 4)
>> for _, i := range n {
>> fmt.Printf("Int: %d\n", i)
>> }
>>
>> See full code in playground <https://goplay.tools/snippet/FwSn1BaWg7k>.
>> On Saturday, October 21, 2023 at 3:15:03 PM UTC-4 Dan Kortschak wrote:
>>
>>> On Sat, 2023-10-21 at 11:58 -0700, Mike Schinkel wrote: 
>>> > Recently I was trying to write a func using generics where I wanted 
>>> > to use a slice of an interface that would contain implementers of 
>>> > that interface and then pass those types to a generic function, but I 
>>> > ran into this error: 
>>> > 
>>> > type MyStruct of MySlice{} does not match inferred type MyInterface 
>>> > for T 
>>> > 
>>> > My code is complex so I wrote the simplest example for this email and 
>>> > here is part of that code: 
>>> > 
>>> > type Suiter interface { 
>>> >     Suit() 
>>> > } 
>>> > func Append[T any](s []T, i T) []T { 
>>> >     return append(s, i) 
>>> > } 
>>> > func main() { 
>>> >     var s []Suiter 
>>> > 
>>> >     //CAN GENERICS SUPPORT THIS? 
>>> >     //s = Append(s, Clubs{}) 
>>> >     //s = Append(s, Hearts{}) 
>>> >     //s = Append(s, Diamonds{}) 
>>> >     //s = Append(s, Spades{}) 
>>> > 
>>> >     //VERSUS HAVING TO DO THIS? 
>>> >     s = Append(s, Suiter(Clubs{})) 
>>> >     s = Append(s, Suiter(Hearts{})) 
>>> >     s = Append(s, Suiter(Diamonds{})) 
>>> >     s = Append(s, Suiter(Spades{})) 
>>> > 
>>> >     for _, suit := range s { 
>>> >         fmt.Printf("Suit: %s\n", suitName(suit)) 
>>> >     } 
>>> > } 
>>> > The full code is here in a playground. 
>>> > 
>>> > Note: My example func Append() makes no sense in real use, I only use 
>>> > it as it should be an easily understood example to show the syntax I 
>>> > am trying to illustrate. 
>>> > 
>>> > Question: Is there a way to write Append() such that I can call 
>>> > Append(s, Clubs{}) instead of having to write Append(s, 
>>> > Suiter(Clubs{}))? 
>>> > 
>>> > And if no, is there a chance that in the future Go generics will be 
>>> > able to support that level of type inference? 
>>> > 
>>>
>>> The pre-generics language handles this case: 
>>> https://go.dev/play/p/jaJF7LTSVYe 
>>>
>>> Is there something about your real case that makes this not acceptable? 
>>>
>>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to golang-nuts...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/golang-nuts/805dbd4a-e2a0-426e-b61f-45b9333803f5n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/adfe6f16-92a5-4195-bcda-2412506c2410n%40googlegroups.com.

Reply via email to