The Language Reference & Guide
- Welcome to Emojicode
- Syntax
- The Basics
- Literals
- Variables and Assignment
- Control Flow
- Classes & Value Types
- Overloading
- Operators
- Optionals
- Errors
- Inheritance and Overriding
- Protocols
- Enumerations
- Types and Namespaces
- Types as Values
- Documentation
- Generics
- Callables
- Packages
- Threads
- Safe and Unsafe Code
- Memory Management
- References
- Appendix: The Emojicode Compiler
Variables and Assignment
An important aspect of programming are variables. Variables pair a name with a value. The variable name can consist of any sequence of characters but may not contain spaces or emojis and may not begin with a number. These are examples of valid variable names:
newYork
incredibly-long-variable-name
send_email_to:george@washington~
There are two types of variables: mutable and constant variables. Constant variables differ from normal ones in that they cannot be changed after they were initially set.
Assigning a Constant Variable
In many cases you will only assign a variable once, i.e. store a value into it, and then use it without ever changing it. These are the cases where you should use a constant variable.
Constant variables are extremely easy to use:
31 β‘οΈ daysInDecember
π€Earthπ€ β‘οΈ thirdPlanet
assignment βΆ expression β‘οΈ [mutable] assignee
assignee βΆ variable | method-call
As you can see from the examples, the value you want to assign is on the left hand side of β‘οΈ
while the variable you want to assign the value to is on the right hand side. The above code therefore sets daysInDecember
to 31
and thirdPlanet
to the string Earth
.
The compiler infers the type of variables from the provided values automatically.
Obviously, you cannot change a constant variable. If you try to reassign a constant variable, this will result in a compiler error.
Declaring and Assigning Mutable Variables
Sometimes, however, you need variables whose values can be modified. This is where mutable variables come in.
Before you can use a mutable variable you need to declare it. There are two ways, either you declare and assign the variable to a value in one step or you explicitly declare the variable, in which case it wonβt have a value initially.
5300 β‘οΈ ππ money
ππ catName π‘
mutable βΆ π [π]
declaration βΆ ππ variable type
The first example declares the variable money
and assigns it the value 5300. The compiler will infer that the type of the variable is π’.
The second line explicitly declares the variable catName
and that it is of the type π‘. It does not have a value until assigned and the compiler will raise an error if you try to use it before having assigned a value.
Changing the value of mutable variables
The point of mutable variables is its inconstancy, so let us see how you can change the value of an mutable variable.
The following is an example of assigning the two variables we declared before:
5300 β‘οΈ π money
π€Kittyπ€ β‘οΈ π catName
Assigning an mutable variable is very similar to assigning a constant one, but we need to note that the variable name is preceded by π. You will get an error if you omit the π and you will get an error if you try to assign a variable this way, that has not been declared.
This mechanism can prevent bugs that could emerge if you, for instance, misspell a variable.
Scoping
Variables are only accessible from the scope in which they were declared. Every code block (everything between a π and π) defines a separate scope. When the code block is exited, this scope is destroyed and so are all variables and values that were declared in it. Furthermore, classes and value types define their own scope.
Operator Assignment
Weβll now have a look at another useful structure: Operator Assignment. Operator Assignment allows you to apply an operator to variable or to be more precise, an operator is applied to the value of a variable and another operand and the result of the operation is then stored into the variable.
operator-assignment βΆ variable β¬
οΈ binary-operator expression
In the following example, the variable i
is first incremented by one, then by 5 and finally divided by 3.
0 β‘οΈ ππi
i β¬
οΈβ 1
i β¬
οΈβ 5
i β¬
οΈβ 3
Operator Assignment can obviously only be used with mutable variables.
Note that the evaluation of operation assignment is strictly equivalent to the following expression where a
is the variable of the assignment, β
the respective operator, and e
the expression.
a β¬
οΈ a β e