bool equality support
This commit is contained in:
@@ -18,12 +18,20 @@ abstract Operand(Either<String, Float>) from Either<String, Float> to Either<Str
|
|||||||
return Right(0.0 + f);
|
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
|
// Doing this one implicitly just wasn't working in conjunction with Lambda.fold
|
||||||
|
|
||||||
/* @:from */
|
/* @:from */
|
||||||
public inline static function fromDynamic(d:Dynamic):Operand {
|
public inline static function fromDynamic(d:Dynamic):Operand {
|
||||||
return switch (Type.typeof(d)) {
|
return switch (Type.typeof(d)) {
|
||||||
case TInt | TFloat: Right(0.0 + 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:
|
default:
|
||||||
if (Std.isOfType(d, String)) {
|
if (Std.isOfType(d, String)) {
|
||||||
Left(d);
|
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()
|
// This wasn't working as an implicit conversion in conjunction with Lambda.fold()
|
||||||
|
|
||||||
/* @:to */
|
/* @:to */
|
||||||
|
|||||||
@@ -71,7 +71,10 @@
|
|||||||
(Assert.isTrue (= 1 1 1 1))
|
(Assert.isTrue (= 1 1 1 1))
|
||||||
(Assert.isFalse (= 1 2 1 1))
|
(Assert.isFalse (= 1 2 1 1))
|
||||||
(Assert.isTrue (= "hey" "hey" "hey"))
|
(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 []
|
(defun _testIf []
|
||||||
(Assert.equals true (if 1 true false))
|
(Assert.equals true (if 1 true false))
|
||||||
|
|||||||
Reference in New Issue
Block a user