1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// implement a type that can be unvalid or valid.
// It's a replacement of Optional, but can be allocated on stack, so no heap allocaton needed.
// When unvalid, it should be not possible to get unvalid value, thats why abstract type is used.
// bool b: true means valid, false unvalid
absviewt@ype valid(a:viewt@ype,b:bool) = (a,bool b)
viewtypedef Valid(a:viewt@ype) = [b : bool] valid(a,b)
// Wraps valid value.
fn {a:viewt@ype} create_valid(a) :<> valid(a,true)
// Creates unvalid type.
fn {a:viewt@ype} create_unvalid() :<> valid(a,false)
// Only function that allows getting value. Only accepts valid values.
fn {a:viewt@ype} unwrap_valid(valid(a,true)) :<> a
castfn destroy_unvalid{a:viewt@ype}(valid(a,false)) :<> void
// Check if value is valid
fn{a:viewt@ype} is_valid{c:bool}(!valid(a,c) >> valid(a,b)) :<> #[b:bool] bool b
|