In general we try hard to make meaning independent of order in a couple of different senses.
First, we strongly avoid making meaning depend on order of execution because it is inherently unpredictable. In this case it’s pretty simple but in general it is intractable (in the formal sense) to decide what will execute before what without actually executing it. Traditional dynamic languages have often been fine with allowing actual execution to determine semantics because they do almost no static analysis of code. A principle in the design of Julia is that the meaning of code should be statically resolvable, which helps both compilers and humans trying to understand the code.
Second, we also try to avoid syntactic order being significant where order is not inherently significant (c.f. it’s inherent to the order of expression evaluation). For example, we’ve been very careful to design things so that the order of import statements doesn’t matter (or more precisely, if you have a program that works without warnings and you reorder the import statements and the program still works without warnings, then it does the same thing). We’ve also been very careful to design things so that the order of method definitions doesn’t matter. This is because people like to reorganize their code by moving things around. And if that can cause subtle changes in meaning, that’s a huge gotcha.
The scope rule is very simple: assignment to
x in a local scope means either
- if an
x local variable exists in an outer scope, it updates it;
- if no
x local variable exists, it creates and assigns
It does not matter if a local
x in an outer scope is created or assigned before or after the assignment to
x that we’re considering the meaning of—in both the syntactic and evaluation order sense of the word. In particular, this also means that these don’t affect the meaning of the code:
- if the assignment
x = 1 is syntactically before or after the definition of
- if the assignment
x = 1 is evaluated before or after the definition of
This means that you can move code around safely and put assignments in conditionals without fear of subtle changes of meaning.