(or) should return the last value even if falsy

This commit is contained in:
2021-11-01 10:51:00 -04:00
parent 43072fd1ec
commit 3d6ea4b397
3 changed files with 25 additions and 12 deletions

View File

@@ -233,23 +233,30 @@ class Macros {
var b = wholeExp.expBuilder();
var uniqueVarSymbol = b.symbol();
var lastValue = args.pop();
b.begin([
b.call(b.symbol("localVar"), [
b.meta("mut", b.typed("Dynamic", uniqueVarSymbol)),
b.symbol("null")
]),
b.call(b.symbol("cond"), [
for (arg in args) {
b.call(
b.symbol("cond"), [
for (arg in args) {
b.call(
b.call(b.symbol("set"), [
uniqueVarSymbol,
arg
]), [
uniqueVarSymbol
]);
}
].concat([
b.call(
b.call(b.symbol("set"), [
uniqueVarSymbol,
arg
]), [
uniqueVarSymbol
]);
}
])
b.symbol("true"), [
lastValue
])
]))
]);
};

View File

@@ -167,7 +167,7 @@ class BasicTestCase extends Test {
}
function testOr() {
Assert.equals(5, BasicTestCase.myOr1);
_testOr();
}
function testAnd() {

View File

@@ -172,7 +172,13 @@
(var myCondFallthrough (cond
(false "not this")))
(var myOr1 (or null 5))
(function _testOr []
(Assert.equals 5 (or null 5))
// If the last value is falsy it can still be returned without breaking
// the expected behavior of or -- which also allows for the (or <optional arg> <default value>)
// idiom for empty arrays and strings
(Assert.equals (.toString []) (.toString (or null [])))
(Assert.equals "" (or null [] "")))
(var myAnd1 (and 5 6))
(var myAnd2 (and false 5 6))