aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/DATS/valid.dats6
-rw-r--r--lib/SATS/valid.sats15
2 files changed, 14 insertions, 7 deletions
diff --git a/lib/DATS/valid.dats b/lib/DATS/valid.dats
index f4ea0aa..c13386e 100644
--- a/lib/DATS/valid.dats
+++ b/lib/DATS/valid.dats
@@ -1,9 +1,9 @@
#define ATS_DYNLOADFLAG 0
staload "lib/SATS/valid.sats"
-assume valid(a:t@ype,b:bool) = (a,bool b)
+assume valid(a:viewt@ype,b:bool) = (a,bool b)
-implement{a} create_valid(value : a) = (value,true)
+implement{a} create_valid(value) = (value,true)
// Create unvalid value by casting null pointer to type.
implement{a} create_unvalid() = let
@@ -13,3 +13,5 @@ in
end
implement{a} unwrap_valid(v) = v.0
+
+implement{a} is_valid(v) = v.1
diff --git a/lib/SATS/valid.sats b/lib/SATS/valid.sats
index e3841e0..83081fe 100644
--- a/lib/SATS/valid.sats
+++ b/lib/SATS/valid.sats
@@ -3,16 +3,21 @@
// 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
-abst@ype valid(a:t@ype,b:bool) = (a,bool b)
+absviewt@ype valid(a:viewt@ype,b:bool) = (a,bool b)
-typedef Valid(a:t@ype) = [b : bool] valid(a,b)
+viewtypedef Valid(a:viewt@ype) = [b : bool] valid(a,b)
// Wraps valid value.
-fn {a:t@ype} create_valid(value : a) : valid(a,true)
+fn {a:viewt@ype} create_valid(a) : valid(a,true)
// Creates unvalid type.
-fn {a:t@ype} create_unvalid() : valid(a,false)
+fn {a:viewt@ype} create_unvalid() : valid(a,false)
// Only function that allows getting value. Only accepts valid values.
-fn {a:t@ype} unwrap_valid(valid(a,true)) : a
+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{b:bool}(!valid(a,b)) : bool b