HaxeFoundation / HaxeFoundation/haxe

[nullsafety] allow indirect field initialization

Open
#7,819 6 comments 0 reactions 1 assignee Claimed by @RealyUniqueName View on GitHub
enhancement feature-null-safety
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

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.