You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Tolk] Union types T1 | T2 | ... and pattern matching
Union types allow a variable to hold multiple possible types.
They are key to message parsing and routing in future struct-based designs.
At the TVM level, they are tagged unions, like enums in Rust.
Pattern matching is the only way of handling union types:
> match (result) {
> slice => { /* result is smart-casted to slice */ }
> UserId => { /* result is smart-casted to UserId */ }
> }
`match` must cover all union cases (should be exhaustive).
It can also be used as an expression.
Variable declaration inside `match` is allowed.
Besides `match`, you can test a union type by `is`:
> if (v is cell) {
> // v is smart-casted to cell
fun okWithManualUnion(a: int8, b: int16): int16 | int8 {
9
+
if (a > 10) {
10
+
return a;
11
+
}
12
+
return b;
13
+
}
14
+
15
+
fun autoInferUnionType(a: int8, b: int16) {
16
+
if (a > 10) {
17
+
return a;
18
+
}
19
+
return b;
20
+
}
21
+
22
+
/**
23
+
@compilation_should_fail
24
+
@stderr function `autoInferUnionType` calculated return type is `int8 | int16`; probably, it's not what you expected; declare `fun (...): <return_type>` manually
0 commit comments