EddTests
EddTests
MModular
Created by EddTests on 9/8/2023 in #questions
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
7 replies