Live data from Hacker News

Interface Upgrades in Go

avtok.com

41–43 of 43 posts

Re: Interface Upgrades in Go

#41
post #38

Earlier quoted context omitted.

Another example of "Wtf were they thinking when they wrote this?" in the Java stdlib is MessageDigest. To build a digester object, you must pass it a string literal to tell it which algorithm to use, and catch a NoSuchAlgorithm exception. Instead of, you know, just using an enum for all supported algorithms.

I think standard Java library API doesn't know what algorithms are supported on particular platform, additionally you can implement your own messagedigest.

They list the algorithms explicitly here:

https://docs.oracle.com/javase/7/docs/api/java/security/Mess...

And here:

https://docs.oracle.com/javase/7/docs/technotes/guides/secur...

Re: Interface Upgrades in Go

#42

Earlier quoted context omitted.

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.

At 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.)

I'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.

Re: Interface Upgrades in Go

#43

Earlier quoted context omitted.

It seemed to me that interfaces have a second function of providing partial uniformity across types (not just abstracting procedures). I.e the Animal interface has a method `Speak() string` and you have a function that takes an Animal that prints something like "Animal type {species} says {speak}" you would have to define a getter on the Animal's species even though its not a really a procedure. But I see what you me…

If you have a function that requires both the species and a speak message from an animal, it would make sense for the interface to have a function that returns both the species and speak strings. I think that this [0] does what you would want. If you want to guarantee that two structs contain a set of the same required fields, I believe the best practice would be to include an anonymous field for a struct with the fi…

That second one is damn interesting, didn't know you could do that. Thanks!
Post reply on HN