aboutsummaryrefslogtreecommitdiff
path: root/lib/SATS
diff options
context:
space:
mode:
authorXander <xander@biltopia.org>2023-08-17 22:08:21 +0200
committerXander <xander@biltopia.org>2023-08-17 22:08:21 +0200
commitd177285a761de33b86fcbde72fd718137bfa6f86 (patch)
tree3a880d6df65418ea591be69cf36fd3da8e679f07 /lib/SATS
parente2d7deb82cd25b47cbccda0c48ab39e17c5c536d (diff)
downloadats-os-d177285a761de33b86fcbde72fd718137bfa6f86.tar.xz
ats-os-d177285a761de33b86fcbde72fd718137bfa6f86.zip
Add valid/unvalid type, replacement of optional
Diffstat (limited to 'lib/SATS')
-rw-r--r--lib/SATS/valid.sats18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/SATS/valid.sats b/lib/SATS/valid.sats
new file mode 100644
index 0000000..e3841e0
--- /dev/null
+++ b/lib/SATS/valid.sats
@@ -0,0 +1,18 @@
+
+// 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
+abst@ype valid(a:t@ype,b:bool) = (a,bool b)
+
+typedef Valid(a:t@ype) = [b : bool] valid(a,b)
+
+// Wraps valid value.
+fn {a:t@ype} create_valid(value : a) : valid(a,true)
+
+// Creates unvalid type.
+fn {a:t@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
+