Hi Mojo team and community,
There have been discussions about assignment syntax and whether Mojo should adopt Python’s walrus operator (:=). Some voices have even suggested removing = entirely.
I believe there is a much cleaner path forward, inspired by Go (Golang).
Go’s Success Story
In Go, the short declaration operator := has become the default and idiomatic way to declare and initialize variables inside functions. Most Go code uses := for the vast majority of local variable declarations. The regular = is used only for reassignment to already-declared variables.
This approach has worked extremely well for over 15 years.
Important clarification: Go is not a scripting language. It is a statically typed, compiled systems programming language used at Google, Uber, Dropbox, Kubernetes, Docker, and many other high-performance projects. The success of := has nothing to do with “scripting language stupidity” — it is a deliberate, thoughtful language design decision.
Why Python’s Walrus Operator Was So Controversial
When Python introduced the walrus operator (:=) in 3.8 as an assignment expression, it caused massive controversy, including contributing to Guido van Rossum temporarily stepping down as BDFL.
The main objections were:
- It broke Python’s long-standing rule that assignment (
=) is a statement, never an expression. - It introduced inconsistency by creating two similar-looking assignment operators with different rules.
- Many felt it sacrificed Python’s core philosophy of “Explicit is better than implicit” and “Readability counts”.
The walrus operator was introduced primarily to prevent a dangerous class of logical bugs: the classic mistake of writing = instead of == inside conditions:
if (x = 5): # Accidentally assigns instead of comparing → always True!
In C/C++ this bug has caused countless subtle logic errors that are hard to debug. Python avoided it for decades by making = a statement only. The walrus operator was their compromise to allow convenient initialization while trying to prevent that bug class.
Today, Python’s walrus operator is accepted as a niche feature. It is used sparingly by many developers, mainly in specific cases, but it never became a commonly used or beloved part of the language.
A Better Solution for Mojo: Go-Inspired Assignment
I propose that Mojo makes := the standard operator for declaration + initialization, similar to Go:
# Declaration + initialization (preferred)
name := input("Name or 'quit': ")
count := len(data)
result := some_complex_function()
# Reassignment only
count = count + 1
name = "new value"
# Clean control flow
if name := input("Name: "); name != "quit":
...
Why This Would Be Excellent for Mojo
- Prevents the dangerous class of logical bugs (
if x = 5instead ofx == 5becomes impossible or very obvious) - High internal consistency (no special walrus-only operator)
- Excellent ergonomics (much less verbose than
var+=) - Still feels familiar to Python developers
- Aligns well with Mojo’s goals as a high-performance, statically typed language
Would the Mojo team and community consider making := the recommended/default way to declare new variables?
I’d especially love to hear from people who have used both Python and Go in larger projects.
Thanks for reading!