HaxeFoundation / HaxeFoundation/haxe

Cannot use structure inititialisation (from `@:structInit`) to declare a new entry in a map

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

Description

Using Haxe version 4.0.5. Here is the content of the file `Main.hx`.

```hx
package;

@:structInit
class Student
{
public final name:String;
public final score:Int;
}

class Main
{
public static function main()
{
var map = new Map();

var student1:Student = { name: "Boris", score: 20 };
map["the best"] = student1;

map["the worst"] = { name: "Vianney", score: -1 }; // Compilation error here
}
}
```

When trying to launch this code using `haxe --run Main`, the compiler gives this error:

```
Main.hx:19: characters 9-58 : No @:arrayAccess function for haxe.ds.Map accepts arguments of String and { score : Int, name : String }
```

We must use a temporary variable to insert a new value in the map when using the way to construct an instance given by `@:structInit`.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure from the provided Main.hx example with Haxe 4.0.5 using haxe --run Main. Start by tracing compiler handling of @:structInit expressions used directly in Map assignment. Done means the direct struct-initialization expression compiles without requiring a temporary variable.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.