mojo
mojo copied to clipboard
[Suggestion]: Remove `let`
Bug description
Mojo would be closer to python without let and var. It seems the compiler could determine from the source whether a variable is declared, and whether it is mutable.
The only loss I can think of is the overloading when there is both a function with a mutable argument and one with a non-mutable argument, but that's very minor.
Steps to reproduce
reproducible
System information
latest mojo
Even if the compiler could track whether a variable was mutated or not, it is still better to be explicit about it. It is the same rationale for explicitly declaring types of parameters to a function and the return type even when they can be inferred.
The explicitness also helps the humans reading the code of the intention. It may not be clear two weeks later that a variable was supposed to be immutable, and someone mutated the value.
Now this being said, I don't think mojo needs both let
and var
. Mojo could take the stand that everything is mutable, and you need to prefix the variable with let
to make it immutable. Or vice versa and assume everything is immutable and prefix with var
to make it mutable. I'd rather mojo default to immutability and having to type out some extra characters to make a variable mutable,
However, in the keyword naming proposal for references, there is talk of removing let.
Also various code editors could make use of the meta information about let and var for syntax coloring/formatting, as well as possible lint checking and various other verification tools used when coding.
All this is true, and I generally err on the side of providing redundant meta data to be safer, but it has to be weighted against "being more like python".
FWIW, I agree with @bserletfun here. We added let and var early on due to experience with other languages, but Mojo has somewhat different language rules that make let pretty unintuitive: you can "overwrite" let constants with new values because they define new lifetimes for the old values. Mojo also has 'alias' declarations which are completely different than let, but are confusingly similar at a glance.
I think that dropping let
entirely would make the language simpler to teach and learn, and I see little downside to it. We can still keep var
which defines a scoped value (unlike pythons implicit declarations)
I could think of this, just making let an alias for var. I think it is sometimes annoying when programming a lot of rust, where it is let
and then coming to go again, where it is var
. Why not handle this like go handles semicolons? I mean, you can use let
but when you format / compile it, it becomes var
again. I'm not sure if this makes it more complex but it could be an option.
Hello, yes i think it is a very good idea to do that aswell:
The let keyword has different significations depending on the languages.
it could allow peoples that comes from any language to not be confused and learn smoothly, without being challenged by previously learned notions
When implementing complexe algorithms, being able to decide if a variable is constant or not helps a lot. I actually made a leetcode exercice recently where using var/let correctly allowed the compiler to tell me there was a var
that was never mutated, hence saving a lot of debugging time. So that help sometimes. I find the compiler messages very helpful most of the time, they help me figure out what should be var
and what should be let
. But maybe I don't have the whole picture, this is just my 2c on the subject. Maybe they should just have different names?
xref also https://github.com/modularml/mojo/discussions/120
To be honest, I really like to be able to differentiate between the type of variable I am declaring using let
and var
. Although I don't care too much about the keywords themselves, I think it's important to retain a way to say, this is constant and this is not. If that means, removing let
and var
and adding const
or something else, so be it.
I am aligned with what @rarebreed said in the above comment:
Now this being said, I don't think mojo needs both let and var. Mojo could take the stand that everything is mutable, and you need to prefix the variable with let to make it immutable. Or vice versa and assume everything is immutable and prefix with var to make it mutable. I'd rather mojo default to immutability and having to type out some extra characters to make a variable mutable,
Another point of confusion that i noticed on the chat:
It is that global variables require to initialize them with =
,
But then in main, we sort of assign the real value to it.
And let
is supposed to be assigned only one time since it is a constant.
That person might have got confused when he/she used the let keyword to declare a global variable.
let x:String = "hello world"
fn main():
#x = "hello world" #error: expression must be mutable in assignment
print(x) #x contains null
It is true that not having to type var each time would leave way more screen space!
Struct would be less cluttered with var too (if possible).
I think this is the right way to go. I wrote this up in a proposal here: https://github.com/modularml/mojo/blob/main/proposals/remove-let-decls.md
and started a discussion thread here: https://github.com/modularml/mojo/discussions/1456
-Chris
Per discussion, we decided that this is the right way to go. The next version of Mojo will still accept let, but will treat it as var and emit a warning. We'll remove it completely in a later release.
let
has been removed thanks @bserletfun