clipperhouse / gen

Type-driven code generation for Go
http://clipperhouse.com/gen/overview/
Other
1.43k stars 90 forks source link

Generic Generics #69

Closed monkeybutter closed 10 years ago

monkeybutter commented 10 years ago

I'm trying to figure out how to create a map[String]Generic where different generic types can be contained in one map exposing their Where(), Count(), Any(), Select(), etc, functions.

Is this something feasible using this Go generics implementation or am I just asking a nonsense question?

freeeve commented 10 years ago

The way you do that in go is with map[string]interface{}, and it sucks because you'll then need to do type assertions when you pull things out. You could design a gen typewriter to detect the type for you in a select, and do things somewhat sanely, but the API would still need to deal with interface{}. On the other hand, you might be able to define a common interface across all of the Generic types you're talking about--in which case you could use a real interface for this (as opposed to an empty interface). I haven't tried it in gen, but I don't see why it wouldn't work.

What is your use case?

monkeybutter commented 10 years ago

Thanks @wfreeman, I'm trying to find a way of storing slices in a map so I can make use of the Go slice indexing feature. It would be something like defining a map[string]interface{"any slice type"} so I can do things like map[string][:100] no matter what type the slice is.

As you said I might have to define it as an empty interface and convert the slices using type assertions, but I still have to figure out what is the best way of doing this.