assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Could not compare null with nullable type

Open yjhmelody opened this issue 2 years ago • 4 comments

Bug description

TS2322: Type '~lib/as-bignum/assembly/integer/u128/u128 | null' is not assignable to type '~lib/as-bignum/assembly/integer/u128/u128'.
     :
 278 │ return value === null || !(value.lo | value.hi);
     │                  ~~~~
     └─ in ~lib/as-bignum/assembly/integer/u128.ts(278,22)

Steps to reproduce

let x: string | null = "foo";
assert(null != x);

WARNING AS226: Expression resolves to unusual type 'usize'.
   :
 3 │ assert(null != x);
   │        ~~~~
   └─ in foo.ts(3,8)

ERROR TS2365: Operator '!=' cannot be applied to types 'usize' and '~lib/string/String | null'.
   :
 3 │ assert(null != x);
   │        ~~~~~~~~~
   └─ in foo.ts(3,8)

FAILURE 1 compile error(s)

let data: u128 | null = u128.Max;
assert(data != null);

ERROR TS2322: Type '~lib/as-bignum/assembly/integer/u128/u128 | null' is not assignable to type '~lib/as-bignum/assembly/integer/u128/u128'.
     :
 327 │ assert(data != null);

The as-bignum u128 has the following operator overload:

  @inline @operator('==')
  static eq(a: u128, b: u128): bool {
    return a.hi == b.hi && a.lo == b.lo;
  }

  @inline @operator('!=')
  static ne(a: u128, b: u128): bool {
    return !u128.eq(a, b);
  }

AssemblyScript version

v0.25 ~ v0.27

yjhmelody avatar Mar 03 '23 15:03 yjhmelody

@HerrCai0907 Hi, there is another kind type error. See the above examples.

TBH, it's huge break change.

yjhmelody avatar Mar 07 '23 07:03 yjhmelody

@MaxGraey Maybe the as-bignum should handle this problem rather than AssemblyScript?

I really don't understand the specific semantics and scope of overloading != and ==,

yjhmelody avatar Mar 07 '23 07:03 yjhmelody

I always feel that we always need non-overloadable reference comparison and overloadable comparison

yjhmelody avatar Mar 07 '23 07:03 yjhmelody

The second issue seems like another problem related with operator overload.

HerrCai0907 avatar Mar 07 '23 13:03 HerrCai0907