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

Ability to pass maps as first parameter in an extension function #815

Open
jeshecdom opened this issue Sep 12, 2024 · 0 comments
Open

Ability to pass maps as first parameter in an extension function #815

jeshecdom opened this issue Sep 12, 2024 · 0 comments

Comments

@jeshecdom
Copy link
Contributor

Currently, the compiler does not allow declaring an extension function over maps:

extends fun testFun(self: map<Int,Int>) {
 ......
}

Hence, the compiler also rejects mutating functions having maps in their self argument:

extends mutates fun testFun(self: map<Int,Int>) {
......
}

Since maps are passed by value, programs like the following do not modify maps:

fun addEntry(m: map<Int,Int>) {
    m.set(1, 10);
}

fun testFun() {
   let m: map<Int,Int> = emptyMap();
   addEntry(m);    // After calling addEntry, m is NOT modified, because addEntry created a copy of m.
}

This means that if a user actually wants to modify a map in some function, the user needs to do workarounds like wrapping the map inside a struct and pass it to a mutating function:

struct MapWrap {
   m: map<Int,Int>
}

extends mutates fun testFun(self: MapWrap) {
   self.m.set(1, 10);
}

It should be possible to pass a map to an extension (and hence mutating) function directly.

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

No branches or pull requests

2 participants