(default <var> <value>)

This commit is contained in:
2024-11-26 16:41:17 -06:00
parent a4fbf27891
commit 72b76831e8
3 changed files with 28 additions and 2 deletions

View File

@@ -1598,6 +1598,13 @@ class Macros {
b.let([b.typed('Array<${typeName}>', arraySymbol), b.list([])], [for (arg in args.slice(1)) b.call(b.field("push", arraySymbol), [arg])].concat([arraySymbol]));
};
k.doc("default", 2, 2, "(default <variable> <value>)");
macros["default"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
b.when(b.callSymbol("isNull", [args[0]]), [b.set(args[0], args[1])]);
};
return macros;
}

View File

@@ -476,6 +476,10 @@ class BasicTestCase extends Test {
function testTypedArrayMacro() {
_testTypedArrayMacro();
}
function testDefault() {
_testDefault();
}
var aNullToPrint = null;
}

View File

@@ -1014,4 +1014,19 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
(function _testTypedArrayMacro []
(let [a (array Float 1 1.5 2)]
(Assert.isTrue (Std.isOfType a Array))
(Assert.isTrue (Std.isOfType (first a) Float))))
(Assert.isTrue (Std.isOfType (first a) Float))))
(function _testDefault []
// TODO it would be nice to allow &mut on the whole list and &final to override it
(let [&mut f false
&mut t true
&mut n null
&mut i 5]
(default f true)
(Assert.equals false f )
(default t false)
(Assert.equals true t)
(default n 5)
(Assert.equals 5 n)
(default i 9)
(Assert.equals i 5)))