3 ms·
class A extends UnionBase; class B extends UnionBase; public UnionBase someMethod(); furthermore, UnionBase might have isA() and isB() if need be. So, stric
by java-man 5y ago
class A extends UnionBase;
class B extends UnionBase;
public UnionBase someMethod();
furthermore, UnionBase might have isA() and isB() if need be.
So, strictly speaking, your statement is incorrect.
- jez 5y agoIn this case UnionBase is not ad hoc: you had to declare a name for it up front. Ad hoc means you can write the union type wherever types can be written. For example, it’s not possible to write this: public (A | B) someMethod(); in Java.
- java-man 5y agoYes, this is the basic benefit of typed languages. I would not want to refactor a 20 year old code base without type safety - I do want to know what's being returned. I also want my tools to know what's being returned, as well as my compiler.
- carlmr 5y agoBut in languages supporting this you do know that it's either A or B and you can use match expressions to deal with both cases. Usually these are the languages with more focus on type safety, like OCaml, Haskell and Rust.
- rixed 5y agoTo be fair, in OCaml, Haskell or Rust the type "A | B" also had to be declared beforehand.
- deleted 5y ago[deleted]
- carlmr 5y agoYes, but it doesn't really change the way you could handle it, and what I wanted to point out is that it actually helps with type safety, enabling you to create total functions.
- Zababa 5y agoNot exactly. For example, in Rust, you can do unions for traits: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=81fb8e831abf5df84944c54c25c562d6 https://play.rust-lang.org/?version=stable&mode=debug&editio.... OCaml has the same system for its objects, where objects are structurally typed.