5 ms·
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 getSom
by bradleyjg 10y 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();
- tazjin 10y agoThe compiler would tell you that it can't disambiguate that and fail. It's a type error.
- bradleyjg 10y agoI 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.
- Filligree 10y agoThe Haskell solution is to require a type annotation if the type is ambiguous. Of course, Java provides no way to do that.
- Manishearth 10y agoWell, 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.
- areyousure 10y agoThis and other questions are discussed at http://stackoverflow.com/a/442291 http://stackoverflow.com/a/442291