Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Assignment to map[Some_Union][]B doesn't work

Open Beefster09 opened this issue 2 years ago • 3 comments

Context

Action :: union {
    world.Action,
    ui.Action,
}
    input_bindings = make(map[Action][]Physical_Input)

    for action, inputs in binds {  // binds is a map[Action][]Physical_Input literal, which I understand is allocated on the stack (it works fine)
        inp_clone := make([]Physical_Input, len(inputs))
        for bind, i in inputs {
            inp_clone[i] = bind
        }
        input_bindings[action] = inp_clone  // input_bindings[action] is still nil, it seems
    }
  • Operating System & Odin Version:
        Odin: dev-2022-11-nightly:382bd876
        OS:   Windows 10 Home Basic (version: 21H2), build 19044.2364
        CPU:  Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
        RAM:  65343 MiB

Expected Behavior

Assigning a slice to a map[union][]T should work

Current Behavior

the slice is still nil

Beefster09 avatar Dec 16 '22 02:12 Beefster09

This appears to affect all functionality of maps with unions of enums as keys.

Beefster09 avatar Dec 16 '22 06:12 Beefster09

Here is a minimal code example, does it capture your use case?

A :: enum { A }
B :: enum { B }
U :: union {A, B}
m: map[U][]int
s := make([]int, 3)
s[0] = 123
m[B.B] = s
fmt.printf("%#v\n", m)

It prints the expected values on my end:

map[B=[
        123,
        0,
        0,
]]

However, I'm using the old map implementation. So please test my example code on latest Odin or test your code on an older Odin version - maybe the new map implementation does not handle this case correctly.

(Also note that make([]T, 0) (when len(inputs) in your code is zero) will create a nil slice)

awwdev avatar Dec 18 '22 00:12 awwdev

The map doesn't work on latest version either. My current workaround is to have two separate maps.

Beefster09 avatar Dec 18 '22 01:12 Beefster09