Hacker News new | ask | show | jobs
by bradleyjg 3376 days ago
Even if you have a language that forces you to assign the return value, what does the compiler do in a situation like:

  Integer getSomething()
  String getSomething()
when the calling site is:

  Object something = obj.getSomething();
3 comments

The compiler would tell you that it can't disambiguate that and fail. It's a type error.
I guess that makes sense. It is similar to:

  void putSomething(Integer i)
  void putSomething(String i)
and

  obj.putSomething(null)
which I think throws a compiler error in java.
The Haskell solution is to require a type annotation if the type is ambiguous.

Of course, Java provides no way to do that.

Well, I presume `String something = getSomething(); Object s = (Object)something;` would be okay.

Note that in Rust we don't have inheritance (not exactly the same kind, at least), so it's rare where a statically typed value can be referred to with a different type. So there are still annoyances in Java with this approach that Rust wouldn't have.

This and other questions are discussed at http://stackoverflow.com/a/442291