environment
- magic 07.2 h
- mojo 25.2.0
I have a simple Client struct. I need to store instances of Client in a dictionary.
But when I try to call a set method (set_var) which changes data, compiler reports error :
invalid call to ‘set_var’: invalid use of mutating method on rvalue of type ‘Client’
if I use a List in place of dictionary, there is no error.
How can I set mutable to be able to call setter ?
Here is a sample code :
sample.mojo
from collections import Dict
from collections import List
@value
struct Client[]():
var var01: Int
fn __init__(out self, var01: Int):
self.var01 = var01
def set_var(mut self, var01: Int):
self.var01 = var01
def get_var(self) -> Int:
return self.var01
def main():
var l01 = List[Client]()
l01.append(Client(1))
l01[0].set_var(2)
print(l01[0].get_var()) # Ok print 2
var d01 = Dict[String, List[Client]]()
d01['ClientA'] = List[Client]()
d01['ClientA'].append(Client(3))
d01['ClientA'][0].set_var(4) # error invalid use of mutating method on rvalue