HaxeFoundation / HaxeFoundation/haxe

safe navigation issue with abstracts.

Open
#12,012 1 comment 0 reactions 0 assignees View on GitHub
bug feature-inline feature-null-safety
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

This code does not compile:
```haxe
package app;

import lib.WeakInt;

typedef User = {
var age:WeakInt;
}

class TestCase {
extern function getUser():Null;

@:nullSafety(Loose)
public function test()
{
var n:Int = 1;
var user = getUser();
return n == user?.age;
}
}
```

I get the error
```
app/TestCase.hx:17: characters 12-26 : Null safety: Cannot assign nullable value here
```

WeakInt is an abstract over Int tot work around the fact that Haxe will generate strict comparisons for ints in PHP and my ints may actually be strings:

```haxe
abstract WeakInt(Int) from Null to Null {
@:op(A > B) static function gt(a:WeakInt, b:WeakInt):Bool;

@:op(A < B) static function lt(a:WeakInt, b:WeakInt):Bool;

@:op(A != B) static inline function weakNotEquals(a:WeakInt, b:WeakInt):Bool
return (a < b) || (a > b);

@:op(A == B) static inline function weakEquals(a:WeakInt, b:WeakInt):Bool
return !(a != b);
}
```
It does compile with ordinary Ints or without nullSafety.
It also compiles with the Safety library's "!." operator but then I lose the WeakInt feature - it generates a strict comparison:
```
return $n === (($_v_ === null ? null : $_v_->age));
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.