I like the idea of having only explicit block scoped variable declarations. See discussion here: https://forum.modular.com/t/block-scope-vs-function-scope-of-variables
But I think var is already well established for this in Mojo. var is not only used for local variable declaration statements but as
- an argument convention,
- an iteration convention,
- an instance variable declaration,
- a closure convention
too.
Therefore I would rather require explicit var declarations than introduce the walrus operator for variable declaration statements. var already implies block scoping.
Dummy example to show off some usages of var:
@fieldwise_init
struct Foo:
var value: Int
def print_doubled(var list: List[Int]):
var text = "doubled: "
for var value in list:
value += value
list.append(value)
text += ",".join(list)
print(text)
def main()
print_doubled([1, 2, 3]) # doubled: 1,2,3,2,4,6
In all usages var has the same meaning: "a locally declared owned value binding“.