Hi gophers,

Apologies if this is covered somewhere, have searched and not found.  

I'm curious about the Go2 generics draft, how does the team envisage the 
code generated for the ShortestPath (without code expansion like C++ 
templates where it's obvious)?  I can come up with two so far.

i) Use reflect to look up method names:

So the basic generic ShortestPath would look a bit like:

func ShortestPath(src,dst interface{}) interface{} {
    srcV := reflect.ValueOf(src)
    edgesM := srcV.MethodByName("Edges")
    edges := edgesM.Call(...) 
    // etc.
}

ii) Autogenerate mutually recursive interfaces:

type _N interface {
    Edges() []_E
}

type _E interface {
    Nodes() []_N
}

func ShortestPath(src,dst _N) []_N  {
    edges := src.Edges()
    // etc.
}

I'm not considering the problems with either approach, like having to wrap 
the actual concrete types or conversions of slice values.

I'm also guessing I'm missing something obvious that makes it easy.

Cheers,

Jamie

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to