4 ms·
> you can't implement Foo<Bar> and Foo<Baz> on the same class. Works for me: import java.util.List; class Test { public void setListBoolean(Lis
by yawaramin 8y ago
> you can't implement Foo<Bar> and Foo<Baz> on the same class.
Works for me:
import java.util.List;
class Test {
public void setListBoolean(List<Boolean> list) {}
public void setListString(List<String> list) {}
public List<Boolean> getListBoolean() { return null; }
public List<String> getListString() { return null; }
public static void main(String[] args) {}
}
- saagarjha 8y agoYour functions are named differently, though.
- int_19h 8y agoYou're using it, not implementing it. Try: class Test implements List<Boolean>, List<String> { ... } For another example, try your snippet, but rename setListBoolean and setListString to just setList (so that they're overloads).
- yawaramin 8y agoOK I see what you mean now, but that is the combination of type erasure and allowing method overloading that is causing the issue, not the erasure itself. For example, look at a language that doesn't allow overloads, like OCaml. Its types are fully erased, allowing very efficient compiled or transpiled code, e.g. https://reasonml.github.io/en/try?rrjsx=false&reason=LYewJgrgNgpgBAFRgZwC5wLxwN4Cg5yzrIyoAyAlmgBRRWoBch91aAThQHYDmAlL5jgApZADooIbgCZq+AnABEAZVLM0CgDRyCdNHN4BuXAF8jQA https://reasonml.github.io/en/try?rrjsx=false&reason=LYewJgr...
- int_19h 8y agoOverloading is orthogonal to all this. The reason why it can't implement two generic interface instantiations on the same class is not because method names clash (even sans overloading, the language can provide means to implement interface methods with differently-named class methods - e.g. C# does that). It's because at runtime, once you erase the type parameter, there's only one interface type, and you cannot implement it twice. The reason why OCaml doesn't have this problem is because it doesn't have runtime type checks of the kind Java does - there's no "instanceof", and there's no downcasts or cross-casts. So it doesn't need reified types, because it doesn't allow the programmer to perform operations that expose the difference. So, for OCaml, type erasure is a sound approach that is consistent with its type system. But not for Java, because of casts and "instanceof".