4 ms·
For all the comments Go gets on its typesystem, I suppose if rt, ok := dst.(ReaderFrom); ok { return rt.ReadFrom(src) } Isn't the most terribl
by thomasahle 12y ago
For all the comments Go gets on its typesystem, I suppose
if rt, ok := dst.(ReaderFrom); ok {
return rt.ReadFrom(src)
}
Isn't the most terrible way anyone has seen pattern matching implemented.
At least it is better than Java's instanceof + cast.
- skybrian 12y agoIt certainly looks weird at first, but is there anything objectively wrong with it?
- zaphar 12y agoFor even more similar look to pattern matching look at Go's type switch. switch i.(type) { case OptionalInterface1: // do some extra thing we allow case OptionalInterface2: // do some other extra thing we allow } It's really good when you have a set of mutually exclusive interfaces or types.
- arnehormann 12y agoYou should use switch v := i.(type) so you can access v as the type in a case.
- jerf 12y agoYou can do a passable impression of sum types that way too: http://www.jerf.org/iri/post/2917 http://www.jerf.org/iri/post/2917
- danieldk 12y agoAt least it is better than Java's instanceof + cast. Except that in a language with generics you have to cast far less, so it is no surprise that Java is less optimized for type casting. (The primary exception being the equals(Object) method, which most classes should have.)
- thomasahle 12y agoI'm not sure I see how generics help at this particular problem. Functional languages have advanced type systems, but still use pattern matching all the time.
- benjiweber 12y agoThere are other ways in Java http://benjiweber.co.uk/blog/2014/05/03/pattern-matching-in-java/ http://benjiweber.co.uk/blog/2014/05/03/pattern-matching-in-... http://benjiweber.co.uk/blog/2014/08/26/deep-pattern-matching-in-java/ http://benjiweber.co.uk/blog/2014/08/26/deep-pattern-matchin...