fn foo(something: bool) {
if something {
5 as float
} else {
"hello"
}
}
What is the return type of this function? I propose we allow this to compile into a structure that is able to tell what type it is. To consume an anonymous union, you can use match to branch (syntax left undecided) or by trait functions if all variants support the call.
fn bar(something: bool) {
let union = foo(something);
match union {
float => std.log.info("it's a number {}", .{ union }),
string => std.log.info("wow a string {}", .{ union }),
}
}
Union types should keep track of what branch created them, since I imagine a lot of mistakes may arise from mistaken type splits. An error involving a union could look like this:
error: cannot access property on 'float | string'
| if num.floor() {
+----|||||||||||
note: anonymous union type created here:
| if random_cond {
+-|| |||||||||||- condition not known at compile time
Milestone: 0.3.0.