HaxeFoundation / HaxeFoundation/haxe
@:structInit can be used to bypass final assignment
Open
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
Assessment
This issue has not been assessed yet.