M
Modular15mo ago
EddTests

error: expression must be mutable in assignment

I'm trying to create a fibonacci struct:
struct Fib:
var a: Int
var b: Int

fn __init__(inout self):
self.a = 0
self.b = 1

fn generate(self) -> Int:
var new_value = self.a + self.b
if self.a > self.b:
self.b = new_value
else:
self.a = new_value

return new_value
struct Fib:
var a: Int
var b: Int

fn __init__(inout self):
self.a = 0
self.b = 1

fn generate(self) -> Int:
var new_value = self.a + self.b
if self.a > self.b:
self.b = new_value
else:
self.a = new_value

return new_value
However it's failing with this error:
@eddtests ➜ /workspaces/mojo-etudes (main) $ mojo ./project-euler/0002.🔥
./project-euler/0002.🔥:14:17: error: expression must be mutable in assignment
self.b = new_value
~~~~^~
./project-euler/0002.🔥:16:17: error: expression must be mutable in assignment
self.a = new_value
~~~~^~
mojo: error: failed to parse the provided Mojo
@eddtests ➜ /workspaces/mojo-etudes (main) $ mojo ./project-euler/0002.🔥
./project-euler/0002.🔥:14:17: error: expression must be mutable in assignment
self.b = new_value
~~~~^~
./project-euler/0002.🔥:16:17: error: expression must be mutable in assignment
self.a = new_value
~~~~^~
mojo: error: failed to parse the provided Mojo
Any hints on what I need to do to be able to update the values in the struct? I thought inout self was enough
5 Replies
ModularBot
ModularBot15mo ago
Congrats @EddTests, you just advanced to level 2!
Jack Clayton
Jack Clayton15mo ago
Hi @EddTests just need to add inout to say the struct is mutable while running that method:
fn generate(inout self) -> Int:
fn generate(inout self) -> Int:
EddTests
EddTestsOP15mo ago
So something like this? If so, I'm still seeing some errors:
struct Fib:
var a: Int
var b: Int

fn __init__(inout self):
self.a = 0
self.b = 1

fn generate(inout self) -> Int:
var new_value = self.a + self.b
if self.a > self.b:
self.b = new_value
else:
self.a = new_value

return new_value
struct Fib:
var a: Int
var b: Int

fn __init__(inout self):
self.a = 0
self.b = 1

fn generate(inout self) -> Int:
var new_value = self.a + self.b
if self.a > self.b:
self.b = new_value
else:
self.a = new_value

return new_value
Errors:
@eddtests ➜ /workspaces/mojo-etudes (main) $ mojo ./project-euler/0002.🔥
./project-euler/0002.🔥:22:25: error: invalid call to 'generate': invalid use of mutating method on rvalue of type 'Fib'
print(Fib().generate())
~~~~~~~~~~~~~~^~
./project-euler/0002.🔥:11:5: note: function declared here
fn generate(inout self) -> Int:
^
mojo: error: failed to parse the provided Mojo
@eddtests ➜ /workspaces/mojo-etudes (main) $ mojo ./project-euler/0002.🔥
./project-euler/0002.🔥:22:25: error: invalid call to 'generate': invalid use of mutating method on rvalue of type 'Fib'
print(Fib().generate())
~~~~~~~~~~~~~~^~
./project-euler/0002.🔥:11:5: note: function declared here
fn generate(inout self) -> Int:
^
mojo: error: failed to parse the provided Mojo
O I'm running it like
fn main() raises:
print(Fib().generate())
fn main() raises:
print(Fib().generate())
Jack Clayton
Jack Clayton15mo ago
Just need to pop it in a variable first:
fn main() raises:
var f = Fib()
let res = f.generate()
print(res)
fn main() raises:
var f = Fib()
let res = f.generate()
print(res)
EddTests
EddTestsOP15mo ago
Thanks Jack!
Want results from more Discord servers?
Add your server