Fix truthy() for objects, make empty lists falsy

This commit is contained in:
2020-11-23 07:05:27 -07:00
parent ccd683ccc3
commit 9445ffd435
3 changed files with 13 additions and 3 deletions

View File

@@ -1,5 +1,7 @@
package kiss; package kiss;
using Std;
class Prelude { class Prelude {
public static function add(a, b) { public static function add(a, b) {
return a + b; return a + b;
@@ -69,13 +71,17 @@ class Prelude {
case TBool: (v : Bool); case TBool: (v : Bool);
default: default:
// Empty strings are falsy // Empty strings are falsy
var str = cast(v, String); if (v.isOfType(String)) {
if (str != null) { var str:String = cast v;
str.length > 0; str.length > 0;
} else if (v.isOfType(Array)) {
// Empty lists are falsy
var lst:Array<Dynamic> = cast v;
lst.length > 0;
} else { } else {
// Any other value is true by default // Any other value is true by default
true; true;
} };
} }
} }

View File

@@ -117,6 +117,8 @@ class BasicTestCase extends Test {
Assert.equals(false, BasicTestCase.myIf6); Assert.equals(false, BasicTestCase.myIf6);
Assert.equals(true, BasicTestCase.myIf7); Assert.equals(true, BasicTestCase.myIf7);
Assert.equals(false, BasicTestCase.myIf8); Assert.equals(false, BasicTestCase.myIf8);
Assert.equals(false, BasicTestCase.myIf9);
Assert.equals(true, BasicTestCase.myIf10);
} }
function testMacros() { function testMacros() {

View File

@@ -62,6 +62,8 @@
(defvar myIf6 (if false true false)) (defvar myIf6 (if false true false))
(defvar myIf7 (if "string" true false)) (defvar myIf7 (if "string" true false))
(defvar myIf8 (if "" true false)) (defvar myIf8 (if "" true false))
(defvar myIf9 (if [] true false))
(defvar myIf10 (if [1] true false))
(defvar :Int myInt 8) (defvar :Int myInt 8)