(or) should return the last value even if falsy
This commit is contained in:
@@ -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
|
||||
])
|
||||
]))
|
||||
]);
|
||||
};
|
||||
|
||||
|
@@ -167,7 +167,7 @@ class BasicTestCase extends Test {
|
||||
}
|
||||
|
||||
function testOr() {
|
||||
Assert.equals(5, BasicTestCase.myOr1);
|
||||
_testOr();
|
||||
}
|
||||
|
||||
function testAnd() {
|
||||
|
@@ -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))
|
||||
|
Reference in New Issue
Block a user