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

merge_in_place and merge functions from maps module raise an error #22564

Closed
diego-garro opened this issue Oct 17, 2024 · 0 comments · Fixed by #22576
Closed

merge_in_place and merge functions from maps module raise an error #22564

diego-garro opened this issue Oct 17, 2024 · 0 comments · Fixed by #22576
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Type System Bugs/feature requests, that are related to the V types system. Unit: vlib Bugs/feature requests, that are related to the vlib.

Comments

@diego-garro
Copy link

diego-garro commented Oct 17, 2024

Describe the bug

Greetings.

The functions can't copy the values from m2 to m1.

Reproduction Steps

    // mmm map declaration as mutable
    mut mmm := map[string]map[string]int{}

    // adding values to the map mmm
    mmm['greet'] = {
        'hello': 0
    }
    mmm['place'] = {
        'world': 1
    }
    mmm['color']['orange'] = 2

    // printing the map mmm
    println(mmm)

    // mmm2 map declaration as const
    mmm2 := {'name': {'Diego': 3}}

    // printing the map mmm2
    println(mmm2)

    // Using the maps module functions
    // use of maps.merge is commented but its behavior is the same as merge_in_place
    // mmm = maps.merge(mmm, mmm2)
    maps.merge_in_place(mut mmm, mmm2)

    // printing again mmm to the standard output
    println(mmm)

Expected Behavior

The output should be:

{'greet': {'hello': 0}, 'place': {'world': 1}, 'color': {'orange': 2}} # mmm
{'name': {'Diego': 3}} # mmm2
{'greet': {'hello': 0}, 'place': {'world': 1}, 'color': {'orange': 2}, 'name': {'Diego': 3}} # mmm after maps.merge_in_place(mut mmm, mmm2)

Current Behavior

Both, merge_in_place and merge raise the same error, which is:

error: cannot copy map: call `move` or `clone` method (or use a reference)
   76 | pub fn merge_in_place[K, V](mut m1 map[K]V, m2 map[K]V) {
   77 |     for k, v in m2 {
   78 |         m1[k] = v
      |                 ^
   79 |     }
   80 | }

Possible Solution

This solution was proposed by iostream.c in the Telegram chanel:

"the way to fix this is using m1[k] = if V is $map { v.clone() } else { v } internally".

Additional Information/Context

I'am just following the v documentation and add some other lines of code to test.

V version

V 0.4.8 72b4804

Environment details (OS name and version, etc.)

OS: Fedora Linux 39
PC: Laptop HP 15aw002la

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@diego-garro diego-garro added the Bug This tag is applied to issues which reports bugs. label Oct 17, 2024
@diego-garro diego-garro changed the title merge_in_place and merge functions from maps module raises an error merge_in_place and merge functions from maps module raise an error Oct 17, 2024
@felipensp felipensp self-assigned this Oct 18, 2024
@felipensp felipensp added Unit: vlib Bugs/feature requests, that are related to the vlib. Unit: Type System Bugs/feature requests, that are related to the V types system. labels Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Type System Bugs/feature requests, that are related to the V types system. Unit: vlib Bugs/feature requests, that are related to the vlib.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants