bigint icon indicating copy to clipboard operation
bigint copied to clipboard

Null<BigInt> comparing with null does not work

Open romamik opened this issue 5 years ago • 3 comments

		var i: Null<BigInt> = null;
		trace(i == null);

this code fails on js with message Cannot read property 'eq' of null. output js is

	var i = null;
	console.log("",i.eq(bigInt(null.toJSNumber())));

This can be worked around by using null == i. This does not look great, but I do not know better solution:

	@:op(A==B) public inline function eq(o:BigInt):Bool {
		return 
			if (this == null && o == null) true;
			else if (this == null || o == null) false; // one is null and other is not
			else this.eq(o);
	}

romamik avatar Jan 26 '20 12:01 romamik

would adding a special @:op rule work?

// put this before `eq`
@:op(A==B) public inline function eqNullable(o:Null<BigInt>):Bool ...

kevinresol avatar Jan 26 '20 12:01 kevinresol

Nope. Now eqNullable is always called.

...
	@:op(A == B) public inline function eqNullable(o:Null<BigInt>):Bool {
		var oImpl: Null<Impl> = cast o;
		return
			if (this == null && oImpl == null) true;
			else if (this == null || oImpl == null) false; // one is null and other is not
			else this.eq(o);
	}
	@:op(A==B) public inline function eq(o:BigInt):Bool return this.eq(o);
...
		var i1: Null<BigInt> = null;
		trace(i1 == 100);
		var i2: BigInt = 10;
		trace(i2 == 100);

outputs

bigint/BigInt.hx:61: eqNullable
src/Day13Pt2.hx:20: false
bigint/BigInt.hx:61: eqNullable
src/Day13Pt2.hx:22: false

romamik avatar Jan 26 '20 13:01 romamik

This looks more like a Haxe problem. I am not sure how to deal with Null<T> in abstract operator overloads...

kevinresol avatar Jan 26 '20 13:01 kevinresol