Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

afaik there's no such thing as an interface for multiple types in Go; i.e. you can have (the equivalent of) `Foo implements Comparable` but not `Bar, Baz implement ConvertFromTo`. `Graph(N, E)` mentions two types, so you can't really express it as an interface, at least not in current Go.


But this is a single interface, with two parameters. It's effectively just a function that returns an interface type.

     Graph(N, E)
specialized with

     Graph(int, int)
is roughly equivalent to:

     interface Graph_int_int


ah, that makes sense! but what about something like

    ConvertFromTo(F,T)
? i mean, we need a single method

    func convert(f F) T
but where should it go (which type should implement it)?


The issue there has nothing to do with generics. You need overloading for what you want (and, IMO, overloading is a bad idea). Don't write code that way. Since conversion is rarely a generic pattern anyways, you just pass a conversion function in.

     func processStuff(type U, V)(a U, b V, convert func(U) V){
         bleh(a, convert(b))
     }

     processStuff(123, "foo", strconv.Itoa)
Adding the parts needed to do generic conversion functions is not a great idea. That way lies madness and SFINAE.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: