Hacker News new | ask | show | jobs
by pjmlp 23 days ago
Yes, but contrary to Go, if you change an interface it will be a compiler error if additional methods are missing, unless they have default implementations.

In Java type assertions are mostly used when writing code pre-generics style, like downcasting from a common subclasse into the actual implementation, not to see if an interface is supported, as it is a given from the type system.

1 comments

It’s pretty common to type check interface implementations in Go using package-level casts.
I know, it is yet another language hack, just like the iota/const dance, instead of proper language constructs.
How is a package level interface check a “language hack”? They are straight forward and provide a user the same compile time guarantees as the Java implements keyword.
Because they are a workaround for what should be a language feature in first place, just like on ML languages to type check structural types.
What exactly isn’t a language feature? Or do you have issues with semantics?

    var _ Foo = (*Bar)(nil)
The statement asserts that Bar struct pointers are assignable to a Foo interface.

I do agree it’s not as clean looking as the Java implements keyword, but it’s already a fairly terse pattern and IMO the inconvenience does not justify introducing new language syntax.