Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map_upsert always overwrites value in the map #4521

Open
Tetralux opened this issue Nov 26, 2024 · 1 comment
Open

map_upsert always overwrites value in the map #4521

Tetralux opened this issue Nov 26, 2024 · 1 comment

Comments

@Tetralux
Copy link
Contributor

Tetralux commented Nov 26, 2024

  • Odin version: ef3d51f, Windows x64.
package main

import "core:fmt"

main :: proc() {
        m: map[string][dynamic]int

        _, array_ptr, found := map_upsert(&m, "hello", nil)
        fmt.println(m, found, array_ptr) // map[hello=[]] false &[]
        append(array_ptr, 1)

        _, array_ptr, found = map_upsert(&m, "hello", nil)
        fmt.println(m, found, array_ptr) // map[hello=[]] true &[] (!!!!!!)
}

The second println should be map[hello=[1]] true &[1].

@Tetralux Tetralux changed the title map_insert always overwrites value in the map map_upsert always overwrites value in the map Nov 26, 2024
@Tetralux
Copy link
Contributor Author

The rationale here is for continuously accessing a key in a map and updating the value, based on the old value, inserting it if it wasn't there.

value_ptr := map_upsert(&m, key)
append(value_ptr, something)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant