odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.41k stars 562 forks source link

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

Open Beefster09 opened 1 year ago

Beefster09 commented 1 year ago

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
    }

Expected Behavior

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

Current Behavior

the slice is still nil

Beefster09 commented 1 year ago

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

awwdev commented 1 year ago

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)

Beefster09 commented 1 year ago

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