Hi
Just wondering what the reasoning was behind making var bindings mutable, like variables in C, and parameters of functions immutable?
Thanks
Hi
Just wondering what the reasoning was behind making var bindings mutable, like variables in C, and parameters of functions immutable?
Thanks
Hello,
for var arguments being mutable,
i think the reason is that the function now owns the value,
(so it can mutate it).
The value did got moved in,
but it is just an var (like any value defined in scope with var foo=1)
Now for the immutable parameters question, im not an expert,
but i think the reason is that parameters need to propagate forward,
also that mojo need to know at the call size whether the call is correct or not.
And keep building on top of an T.
If parameters could change, it would not scaffold well i think,
because its not possible to assume anything lol.
Consider this:
def foo[T:Int]()->Int where T<10:
T*=2
# Here we lost T !
# So the correct way(i think) is to create another value that derives from T
comptime T2 = T*2
Or this:
def foo[T:Int](arg:Int)->Int:
if arg>5:
T*=2 #Not possible, arg is known after program starts !
return T
var means an ownership transfer, which means that the function is now responsible for either transferring ownership of the value to something else or destroying the value. Destroying the value is the main thing that mandates a mutable binding.