nt/tests/maybe.nix
2026-01-27 17:48:26 +10:00

82 lines
1.7 KiB
Nix

{this, ...}: let
inherit
(this)
ntTrapdoorKey
ntDynamicTrapdoorKey
;
inherit
(this.std)
enfImpls
;
inherit
(this.trapdoor)
mkTrapdoorFn
mkTrapdoorSet
openTrapdoor
;
in {
# NOTE: Maybe is used to simplify parsing Type/Class declarations
# NOTE: and therefore must be implemented manually
Maybe = let
meta = instance: {
sig = "nt::&Maybe";
derive = [];
ops = {};
req = {"nt::&Maybe" = ["unwrap"];};
};
in
mkTrapdoorFn {
default = {
unwrap = T:
assert enfImpls "nt::&Maybe" T "nt::&Maybe.unwrap";
(T |> openTrapdoor ntTrapdoorKey).ops."nt::&Maybe".unwrap;
};
unlock.${ntTrapdoorKey} = meta false;
};
Some = let
meta = instance: {
inherit instance;
sig = "nt::Some";
derive = ["nt::&Maybe"];
ops = {
"nt::&Maybe".unwrap = f: self: f self.${ntDynamicTrapdoorKey}.value;
};
req = {};
};
in
mkTrapdoorFn {
default = value:
mkTrapdoorSet {
default = {};
unlock = {
${ntTrapdoorKey} = meta true;
${ntDynamicTrapdoorKey} = {
inherit value;
};
};
};
unlock.${ntTrapdoorKey} = meta false;
};
None = let
meta = instance: {
inherit instance;
sig = "nt::None";
derive = ["nt::&Maybe"];
ops = {
"nt::&Maybe".map = f: self: self;
};
req = {};
};
in
mkTrapdoorFn {
default = mkTrapdoorSet ntTrapdoorKey {
default = {};
unlock = meta true;
};
unlock.${ntTrapdoorKey} = meta false;
};
}