T1
- T2
- public interface Union<T1,T2>
A union type is a type which can be cast to one of its union-ed types. For instance:
Union<String, Number> u = ...; String s = union(u); // or: Number n = union(u);
Note that Java will not complain when casting to a type that is not part of the union, but the JSweet transpiler will.
Union<String, Number> u = ...; Date d = union(u); // compiles in pure Java, but not in JSweet
When a method expects a union, such as the following m
method:
void m(Union<String, Number> p);Then it can be called with a String or a Number, by first translating to a union:
String s = ... m(union(s));
In case of union types of more than two types, one can extend the
Union
interface for accepting more type parameters. For
instance, to define a union type that takes 3 arguments, just write:
public interface Union3extends Union