v0.0.3: no untagged or mixed tagged unions anymore
This commit is contained in:
20
spec.md
20
spec.md
@@ -84,26 +84,26 @@ def example3() : User {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## mixed-tagged union types
|
## (tagged) union types
|
||||||
```
|
```
|
||||||
type Option[t] =
|
type Option[t] =
|
||||||
'Err Unit # Unit that has tag enum 'Err
|
'Err # If no type specified after tag, defaults to Unit
|
||||||
| t # has no tag
|
| 'Some t
|
||||||
|
|
||||||
# the tags of unions are weakly attached to the types, but won't decay unless they have to *)
|
# the tags of unions are weakly attached to the types, but won't decay unless they have to
|
||||||
def example(n: Num) : Num {
|
def example(n: Num) : Num {
|
||||||
let x = 'MyTag n # type of x is 'MyTag Num
|
let x = 'MyTag n # type of x is 'MyTag Num
|
||||||
x # tag gets removed because target type is Num
|
x # tag gets removed because target type is Num
|
||||||
}
|
}
|
||||||
|
|
||||||
def example2(n: Num) : Option[Num] {
|
def example2(n: Num) : Option[Num] {
|
||||||
n # this works, because the `t` is not tagged in option
|
'Some n
|
||||||
}
|
}
|
||||||
|
|
||||||
def example3-invalid() : Option[Num] {
|
def example3-invalid() : Option[Num] {
|
||||||
Unit # error: can't convert type `Unit` into type `'Err Unit | t`
|
Unit # error: can't convert type `Unit` into type `'Err Unit | 'Some Num`
|
||||||
# Either label the expression with 'Err,
|
# Either label the expression with 'Err,
|
||||||
# or change the return type to Option[Unit]
|
# or change the return type to Option[Unit], and label the expression with 'Some
|
||||||
}
|
}
|
||||||
|
|
||||||
def exampe4(): Option[Num] {
|
def exampe4(): Option[Num] {
|
||||||
@@ -116,8 +116,8 @@ def exampe4(): Option[Num] {
|
|||||||
def example5-error(): Option[Num] {
|
def example5-error(): Option[Num] {
|
||||||
let x = ( 'Err Unit ) :: Option[Unit]
|
let x = ( 'Err Unit ) :: Option[Unit]
|
||||||
x
|
x
|
||||||
# error: can't convert type `'Err Unit | Unit` into type `'Err Unit | Num`
|
# error: can't convert type `'Err Unit | 'Some Unit` into type `'Err Unit | 'Some Num`
|
||||||
# The case `Unit` does not exist in the target `'Err Unit | Num`
|
# The case `'Some Unit` does not exist in the target `'Err Unit | 'Some Num`
|
||||||
}
|
}
|
||||||
|
|
||||||
def example6-error(): Option[Unit] {
|
def example6-error(): Option[Unit] {
|
||||||
@@ -126,7 +126,7 @@ def example6-error(): Option[Unit] {
|
|||||||
# in this case, the enum tag does not decay, like in `example`,
|
# in this case, the enum tag does not decay, like in `example`,
|
||||||
# because we are casting to an enum
|
# because we are casting to an enum
|
||||||
|
|
||||||
# error: can't convert type `'Error Unit` into type `'Err Unit | Num``
|
# error: can't convert type `'Error Unit` into type `'Err Unit | 'Some Num``
|
||||||
# 1st possible solution: manually cast to just `Unit` (via `expr :: Unit`), so that it can convert to the second case of the target
|
# 1st possible solution: manually cast to just `Unit` (via `expr :: Unit`), so that it can convert to the second case of the target
|
||||||
# 2nd possible solution: pattern match against the enum, to rename the tag from 'Error to 'Err
|
# 2nd possible solution: pattern match against the enum, to rename the tag from 'Error to 'Err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user