Skip to content

Commit

Permalink
Update union_types.md
Browse files Browse the repository at this point in the history
improve documentation on union types
  • Loading branch information
robertmuth authored Dec 5, 2023
1 parent f864391 commit a69dd86
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions FrontEndDocs/union_types.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Union Types
# Union/Sum Types

## Basics

Expand All @@ -11,14 +11,16 @@ Example:
```
(type UntaggedUnion1 (union @untagged [ s32 void (ptr @mut s32)]))
```
This represents a union of 3 fields/types: s32, void and (ptr @mut s32).
Note that void is valid field/type for unions.


A `tagged union` can be thought of as a pair of
* a `tag` (a small interger of type `typeid`) and
* an `untagged union`.

Given a tagged union the two components can be retrieved using
`ExprSumTag` and `ExprSumUntagged`.
`sumtypetag` and `sumuntagged`.


Example:
Expand Down Expand Up @@ -49,7 +51,7 @@ Union types are auto flattening, duplicate eliminating and order independent:

## Initialization and Implicit Widening

Union u typed variable, parameter, field-element, returns, etc. can be initialized using
A typed variable, parameter, field-element, return, etc. of type union u can be initialized using
an expression whose type is
* any of the underlying types of u
* another union v where
Expand All @@ -61,7 +63,7 @@ This amounts to an explicit type widening which can also be made explicit using

## Equality Testing

Tagged unions support equality testing against values of the underlying types.
Only tagged unions support equality testing only against values of the underlying types.

Assuming
```
Expand All @@ -85,4 +87,26 @@ The following expressions are valid:

## Narrowing

TBD
Narrowing of values of a union type is possible with `ExprNarrow`.
For `tagged unions` the narrowing can be explicitly marked as `@unchecked`.
For `untagged unions` there is nothing to check anyway.

In order to avoid runtime type inspection narrowing is subject to restriction show below,
assuming the underlying types of v are a subset of the underlying types of u:

### case: u, v are untagged unions

This is always valid:
```
... (narrowto u (typeof v)) ...
```
The value of the expression are the bits of u truncated to `(sizeof (typeof v))`

### case: u, v are tagged unions, unchecked

TBD

###case: u, v are tagged unions, (checked)

TBD

0 comments on commit a69dd86

Please sign in to comment.