ergonomic macro for making variadic typed arrays

This commit is contained in:
2024-04-01 13:16:07 -06:00
parent de4f841d4d
commit 384c172680
3 changed files with 19 additions and 1 deletions

View File

@@ -1579,6 +1579,15 @@ class Macros {
]);
}
k.doc("array", 1, null, "(array <element Type> <elements...>)");
macros["array"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
var arraySymbol = b.symbol();
var typeName = Prelude.symbolNameValue(Helpers.expandTypeSymbol(args[0], k));
b.let([b.typed('Array<${typeName}>', arraySymbol), b.list([])], [for (arg in args.slice(1)) b.call(b.field("push", arraySymbol), [arg])].concat([arraySymbol]));
};
return macros;
}

View File

@@ -460,6 +460,10 @@ class BasicTestCase extends Test {
function testRedefineWithObjectArgs() {
_testRedefineWithObjectArgs();
}
function testTypedArrayMacro() {
_testTypedArrayMacro();
}
var aNullToPrint = null;
}

View File

@@ -1009,4 +1009,9 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
(voidFuncWithObjectArgs "hey" (object num 5 num2 0.5))
(Assert.equals "hey5.5" (funcWithObjectArgs "hey" (object num 5 num2 0.5)))
(Assert.equals "hey5.5" (funcWithObjectArgs "hey" (object num2 4.5 num 1)))
(Assert.equals null (funcWithOptObjectArgs)))
(Assert.equals null (funcWithOptObjectArgs)))
(function _testTypedArrayMacro []
(let [a (array Float 1 1.5 2)]
(Assert.isTrue (Std.isOfType a Array))
(Assert.isTrue (Std.isOfType (first a) Float))))