bool equality support

This commit is contained in:
2020-12-04 14:53:01 -07:00
parent 4074582f94
commit 4504ef37ef
2 changed files with 20 additions and 1 deletions

View File

@@ -18,12 +18,20 @@ abstract Operand(Either<String, Float>) from Either<String, Float> to Either<Str
return Right(0.0 + f);
}
@:from inline static function fromBool(b:Bool):Operand {
return if (b == true) Right(Math.POSITIVE_INFINITY) else Right(Math.NEGATIVE_INFINITY);
}
// Doing this one implicitly just wasn't working in conjunction with Lambda.fold
/* @:from */
public inline static function fromDynamic(d:Dynamic):Operand {
return switch (Type.typeof(d)) {
case TInt | TFloat: Right(0.0 + d);
// Treating true and false as operands can be useful for equality. In practice, no one should use them
// as operands for any other reason
case TBool:
fromBool(d);
default:
if (Std.isOfType(d, String)) {
Left(d);
@@ -59,6 +67,14 @@ abstract Operand(Either<String, Float>) from Either<String, Float> to Either<Str
};
}
@:to public inline function toBool():Null<Bool> {
return switch (this) {
case Right(f) if (f == Math.POSITIVE_INFINITY): true;
case Right(f) if (f == Math.NEGATIVE_INFINITY): false;
default: null;
}
}
// This wasn't working as an implicit conversion in conjunction with Lambda.fold()
/* @:to */

View File

@@ -71,7 +71,10 @@
(Assert.isTrue (= 1 1 1 1))
(Assert.isFalse (= 1 2 1 1))
(Assert.isTrue (= "hey" "hey" "hey"))
(Assert.isFalse (= "hey" "you" "hey")))
(Assert.isFalse (= "hey" "you" "hey"))
(Assert.isTrue (= true true true))
(Assert.isFalse (= true false true))
(Assert.isTrue (= false false false)))
(defun _testIf []
(Assert.equals true (if 1 true false))