tact icon indicating copy to clipboard operation
tact copied to clipboard

Ability to pass maps as first parameter in an extension function

Open jeshecdom opened this issue 5 months ago • 0 comments

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.

jeshecdom avatar Sep 12 '24 09:09 jeshecdom