Fix truthy() for objects, make empty lists falsy
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package kiss;
|
||||
|
||||
using Std;
|
||||
|
||||
class Prelude {
|
||||
public static function add(a, b) {
|
||||
return a + b;
|
||||
@@ -69,13 +71,17 @@ class Prelude {
|
||||
case TBool: (v : Bool);
|
||||
default:
|
||||
// Empty strings are falsy
|
||||
var str = cast(v, String);
|
||||
if (str != null) {
|
||||
if (v.isOfType(String)) {
|
||||
var str:String = cast v;
|
||||
str.length > 0;
|
||||
} else if (v.isOfType(Array)) {
|
||||
// Empty lists are falsy
|
||||
var lst:Array<Dynamic> = cast v;
|
||||
lst.length > 0;
|
||||
} else {
|
||||
// Any other value is true by default
|
||||
true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -117,6 +117,8 @@ class BasicTestCase extends Test {
|
||||
Assert.equals(false, BasicTestCase.myIf6);
|
||||
Assert.equals(true, BasicTestCase.myIf7);
|
||||
Assert.equals(false, BasicTestCase.myIf8);
|
||||
Assert.equals(false, BasicTestCase.myIf9);
|
||||
Assert.equals(true, BasicTestCase.myIf10);
|
||||
}
|
||||
|
||||
function testMacros() {
|
||||
|
||||
@@ -62,6 +62,8 @@
|
||||
(defvar myIf6 (if false true false))
|
||||
(defvar myIf7 (if "string" true false))
|
||||
(defvar myIf8 (if "" true false))
|
||||
(defvar myIf9 (if [] true false))
|
||||
(defvar myIf10 (if [1] true false))
|
||||
|
||||
(defvar :Int myInt 8)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user