HaxeFoundation / HaxeFoundation/haxe
[nullsafety] allow indirect field initialization
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
As @Simn said this probably won't be solved soon, but I feel like this is going to come up a lot (at least it's a very common pattern in vshaxe). In this example, some of the initialization logic is moved outside of the constructor (usually done because that function is also needed in other places), but still called by it.
I guess it's debatable whether it should be allowed if `init()` is overridable, but it seems like perfectly legit code if it's `inline` (*) or `final`.
```haxe
@:nullSafety
class Main {
static function main() {
new Main();
}
var foo:Int; // Field "foo" is not nullable thus should have an initial value or should be initialized in constructor.
function new() {
init(); // Cannot call method init until all instance fields are initialized.
}
function init() {
foo = 0;
}
}
```
None of the solutions (make the field `Null`, init it with some dummy value, add `@:nullSafety(Off)` to it) feel very clean to me.
_(*) actually, looks like this already works with `inline`, but that seems more like a happy accident than an intentional feature. :D_
Contributor guide
Assessment
This issue has not been assessed yet.