Mojo Error: implicit declaration of 'x' is not allowed
error: implicit declaration of 'x' is not allowed; add 'var' to declare a new name means you assigned a value to a name that doesn’t exist yet. In Python that’s how you create a variable. In Mojo, a new variable needs var in front of it. If you’re coming from Python, this is very likely the first Mojo error you’ll see. For the other errors in this series, see Mojo Errors Explained.
The error message
Section titled “The error message”def main(): count = 0 print(count)count.mojo:2:5: error: implicit declaration of 'count' is not allowed; add 'var' to declare a new name count = 0 ^~~~~ varmojo: error: failed to parse the provided Mojo source moduleThe line under the ^~~~~ marks shows the fix: Mojo would put var in front of count. The last line only says the build stopped; the first error: line is the one to read.
What it means
Section titled “What it means”A declaration creates a new name. An implicit declaration creates one without saying so, just by assigning to it, like count = 0 in Python. Mojo turned that off. Every new variable has to be declared explicitly with var:
var count = 0createscount.count = 5changes acountthat already exists.
So when Mojo sees name = value and there’s no name yet, it stops and asks you to say what you meant.
Why Mojo does this
Section titled “Why Mojo does this”It protects you from a classic Python bug. Say you have a total and you mistype it once:
total = 0for n in range(4): totl = total + n # Python: quietly makes a second variableprint(total) # prints 0, not 6Python runs this without complaint and prints the wrong answer. In Mojo, totl = ... is an error, so the typo is caught before the program even runs. The variables page of the Mojo manual explains declared variables in more detail.
How to fix it
Section titled “How to fix it”Fix 1: Add var the first time you use the name
Section titled “Fix 1: Add var the first time you use the name”This is the right fix almost every time. Put var in front of the first assignment, and only there:
def main(): var count = 0 count = count + 1 count += 1 print(count)Output:
2Line 2 creates count. Lines 3 and 4 change it, so they don’t get var.
Fix 2: Fix the typo
Section titled “Fix 2: Fix the typo”If you meant to change an existing variable, the name on the left is probably misspelled. Mojo can’t tell that you meant total, so it suggests var, which would be wrong here:
def main(): var total = 0 for n in range(4): totl = total + n print(total)sum.mojo:4:9: error: implicit declaration of 'totl' is not allowed; add 'var' to declare a new name totl = total + n ^~~~ varmojo: error: failed to parse the provided Mojo source moduleBefore you add var, look at the name. Here the fix is the spelling, not var:
def main(): var total = 0 for n in range(4): total = total + n print(total)Output:
6A quick test: if the variable should still exist after the loop or the if, it should have been declared before it, so the line inside only needs =.
Fix 3: Put var in front of the whole line
Section titled “Fix 3: Put var in front of the whole line”A type annotation or tuple unpacking doesn’t change the rule. age: Int = 36 and x, y = 1, 2 both give this error when the names are new. Put one var at the start of the line:
def main(): var age: Int = 36 var x, y = 1, 2 print(age, x, y)Output:
36 1 2Once x and y exist, swapping them works just like in Python, without var:
def main(): var x = 1 var y = 2 x, y = y, x print(x, y)Output:
2 1You can also declare a variable first and give it a value later. var age: Int on one line and age = 36 on the next is fine, as long as you set it before you read it. If you don’t, you get use of uninitialized value.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- Code pasted from Python. Every first assignment needs
var. Go through the function and add it where each name first appears. - A typo inside a loop or an
if. As in Fix 2, the typo makes a “new” name. Fix the spelling. - Code at the top of a file. Even with
var, a variable outside any function is a different error, global variables are not supported. Put the code insidemain, or usecomptimefor a constant. - A typo with
+=.totl += 1doesn’t create anything, so it gives use of unknown declaration ‘totl’ instead.
Related errors
Section titled “Related errors”- use of unknown declaration ‘x’: you used a name that doesn’t exist (for example a typo with
+=, or reading it). - expressions must not appear at file scope: your code is outside a function.
- use of uninitialized value ‘x’: the variable is declared but has no value yet.
- All Mojo error pages: the full list.
Practice
Section titled “Practice”1. Port a Python snippet
Section titled “1. Port a Python snippet”This is Python code pasted into a Mojo main. Make it build and print Ada scored 27.
def main(): name = "Ada" scores: List[Int] = [9, 8, 10] total = 0 for s in scores: total += s print(name, "scored", total)Hint: three names are new. The loop variable s is created by for, so it doesn’t need var.
Show solution
def main(): var name = "Ada" var scores: List[Int] = [9, 8, 10] var total = 0 for s in scores: total += s print(name, "scored", total)Output:
Ada scored 27name, scores and total are each created once, so each gets one var. total += s changes an existing variable, and for declares s for you.
- The error means you assigned to a new name without
var. - Write
varonce, the first time a name appears. Later changes use plain=. - If you meant to change an existing variable, check the spelling before adding
var. - Annotations and unpacking need it too:
var age: Int = 36,var x, y = 1, 2. - Declaring the same name twice is a different error,
invalid redefinition.
Next up: expressions must not appear at file scope, the error for code that isn’t inside a function.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Variables in the Mojo manual: declared variables, type annotations and late initialization.
- Mojo tips for Python devs: the official list of Python habits that work differently in Mojo.
- Mojo language basics: a short tour of variables, functions and structs.
- Mojo Variables: our lesson on
var, types and changing values.