The Language Reference & Guide
- Welcome to Emojicode
- The Basics
- Variables and Assignment
- Control Flow
- The s package
- Classes & Value Types
- Error Handling
- Inheritance and Overriding
- Types and Namespaces
- Types As Values
- Safe and Unsafe Code
- Memory Management
- 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
thirdPlanet to the string
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 were 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 🔡
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.
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.
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.