HaxeFoundation / HaxeFoundation/haxe

@:structInit can be used to bypass final assignment

Open
#10,017 0 comments 0 reactions 0 assignees View on GitHub
bug feature-structInit
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Putting `@:structInit` on a child class auto-generates a constructor that doesn't initialize the `final` fields of the base class, which seems unsound:

```haxe
function main() {
final child:Child = {s: ""};
trace(child.i); // undefined
}

class Base {
public final i:Int;

public function new(i:Int) {
this.i = i;
}
}

@:structInit class Child extends Base {
public final s:String;
}
```

If `Base` is itself a `@:structInit` class without an explicitly declared constructor, you get the compile-time error you would probably expect:

```
source/Main.hx:3: characters 23-30 : Object requires field i
```

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.