3 ms·
I wish there were a way to create custom data structures without casting to and from interface{} all the time. Heck, it would already help if there were a short
by codeflo 13y ago
I wish there were a way to create custom data structures without casting to and from interface{} all the time. Heck, it would already help if there were a shorthand for interface{}, like "any" or something.
- krakensden 13y agoThe usual pattern is to use a type that requires the thing you pass in to have methods that you use for the data structure, like sort.Interface[1]. It's faster, safer, and better than using interface{}. As for shorthand, behold! type any interface{} [1]: http://golang.org/pkg/sort/#Interface http://golang.org/pkg/sort/#Interface
- codeflo 13y agoThat introduces a new named type though, i.e., the "any" in your package is different from the "any" in mine, which is not what I want. (Unless I'm mistaken here, which might very well be the case.)
- krakensden 13y agoCheck it out: http://play.golang.org/p/l9yn0PRbrd http://play.golang.org/p/l9yn0PRbrd Anyway, that's the point of go's type inference- if the object implements the necessary parts of the interface, it counts as that kind of object.