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
There is some confusion on the nature of choice types. We should clarify it in the Rune docs.
The intention for choice types is to be an implementation of tagged unions (also called discriminated unions, or sum types).
Note that this is different from a classical set union, since for each element in the set we remember the original set where it came from. In terms of set operations:
-- "Set" union between types A and BAUB-- (where U represents the union operator in set theory)-- "Tagged" union between types A and B
({TAG_A} x A) U ({TAG_B} x B)
-- (where x represents the cartesian product in set theory)
Note that the tag allows you to discriminate between instances of different type, which is why most modern programming languages usually support tagged unions rather than actual unions, with one prime exception being Rust.
Note also that, because of this tag, whether the union is inclusive or exclusive is irrelevant - the sets on each side never overlap.
typeStringOrNumber=string|numberfunctionf(input: StringOrNumber){if(typeofinput==='string'){// we know `input` is of type `string`. Note that the `typeof` operator acts as the tag here.}else{// we know `input` is of type `number`.}}
dataStringOrNumber=StringString | NumberDecimalf::StringOrNumber->IO()
f (String stringValue) =-- we know the input is of type `String`. Note that the tag is explicitly defined here.
f (Number numberValue) =-- we know the input is of type `Decimal`
In that same fashion, we support choice types in Rune:
choice StringOrNumber:
string
number
func F:
inputs:
input StringOrNumber (1..1)
output:
result string (1..1)
set result:
input switch
string then // we know `input` is of type `string`. Note that the tag of the union coincides with the name of the type.
number then // we know `input` is of type `number`
The text was updated successfully, but these errors were encountered:
There is some confusion on the nature of choice types. We should clarify it in the Rune docs.
The intention for choice types is to be an implementation of tagged unions (also called discriminated unions, or sum types).
Note that this is different from a classical set union, since for each element in the set we remember the original set where it came from. In terms of set operations:
Note that the tag allows you to discriminate between instances of different type, which is why most modern programming languages usually support tagged unions rather than actual unions, with one prime exception being Rust.
Note also that, because of this tag, whether the union is inclusive or exclusive is irrelevant - the sets on each side never overlap.
Similar implementations:
In that same fashion, we support choice types in Rune:
The text was updated successfully, but these errors were encountered: