Donβt bother adding these decorators. The effects are minimal and only useful in very specific cases. If used improperly they might even harm performance.
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
Control Flow
Emojicode provides different types of control flow statements that allow you to structure the flow of the program.
ππ Code Block
Code blocks are used in conjunction with control flow structures to group statements that will be executed only under if a condition is met or not met or that will be repeated.
Syntactic definition:
block βΆ π statements π
statements βΆ statement statements | statement
Examples of blocks can be seen below.
βͺοΈ If
The βͺοΈ statement is very important. It allows for conditional execution of a code block. The whole syntax is:
if βΆ βͺοΈ condition decorator block [else-ifs] [else]
else-ifs βΆ else-if else-ifs | else-if
else-if βΆ π
ββͺοΈ condition decorator block
else βΆ π
β decorator block
condition βΆ expression | assignment
If the condition evaluates to π, the code block will be executed, and if it evaluates to π it'll be ignored.
This example will display βa is greater than bβ if the content for variable a* is greater than *b:
βͺοΈ a βΆοΈ b π
π π€a is greater than bπ€βοΈ
π
π
π extends an βͺοΈ statement to execute an additional code block in case the expression in the if statement evaluates to false. For example, the following code would display βa is greater than bβ if a is greater than b, and βa is not greater than bβ otherwise:
βͺοΈ a βΆοΈ b π
π π€a is greater than bπ€βοΈ
π
π
π
π π€a is not greater than bπ€βοΈ
π
The π statements is only executed if the βͺοΈ statement evaluated to false, and if all π βͺοΈ statements evaluated to false too.
π βͺοΈ
π βͺοΈ extends an βͺοΈ statement to execute different statements in case the original βͺοΈ condition evaluates to π. However, unlike βͺοΈ, it will execute that alternative expressions only if the βͺοΈ expression is π. For example, the following code would display βa is greater than bβ, βa is equal to bβ, or βa is smaller than bβ:
βͺοΈ a βΆοΈ b π
π π€a is greater than bπ€βοΈ
π
π
βͺοΈ a π b π
π π€a is equal to bπ€βοΈ
π
π
π
π π€a is smaller than bπ€βοΈ
π
The π βͺοΈ statement is only executed if the preceding βͺοΈ expression and any preceding π βͺοΈ expressions evaluated to π, and the current π βͺοΈ expression evaluated to π.
π For In
The π statement allows you to quickly iterate over an instance, that is repeatedly retrieving values from it until there are no more values to provide. For example, you can iterate over an π¨ instance and youβll receive all elements contained in the list. The π statement can iterate over instances of any type which conforms to the ππElementπ protocol.
Its syntax is:
for-in βΆ π variable expression block
The compiler then transforms the statement into byte code equivalent to the statement rewritten to
π‘ iterableβοΈ β‘οΈ iterator
π π½ iteratorβοΈ π
π½ iteratorβοΈ β‘οΈ variable
π The provided block is executed here
π
where iteratable is the instance to iterate over (the result from evaluating the expression) and variable the variable name provided. Evidently, the variable will be of the type that was provided to the generic argument Element when the type of iterable declared its conformance to ππElementπ.
Letβs take a look at an example:
πΏπ€treeπ€ π€beeπ€ π€leeπ€ π€meπ€π β‘οΈ list
π name list π
π nameβοΈ
π
In this example, the code block will be repeated for every value of the list and the values tree
, bee
, lee
, and me
will be printed. The type of name
is naturally π‘. Thatβs due to the fact that π¨πElement declared its conformance to π as ππElementπ
and therefore also returns an iterator of type π‘πElementπ
from which the type of the variable is inferred.
Interlude: β© Ranges
The s package provides a type β©, representing a range. A range is an immutable sequence of integers and is defined by three values: start, stop and step.
If step
is positive, every number f(x) = start + x * step
that matches the constraint start β€ f(x) < stop
is an element of the range. If step
is negative the constraint stop < f(x) β€ start
applies instead.
Ranges are helpful in if you need to repeat something for a specific number of times:
π i πβ© 0 10 2βοΈ π
π π‘ iβοΈβοΈ π Prints numbers 0 through 8 (including).
π
π i πβ© 0 10βοΈ π
π π‘ iβοΈβοΈ π Prints numbers 0 through 9 (including).
π
π i πβ© 10 0βοΈ π
π π‘ iβοΈβοΈ π Prints numbers 10 through 1 (including).
π
π i πβ© 100 -10 -10βοΈ π
π π‘ iβοΈβοΈ π Prints numbers 100 through 0 (including).
π
π Repeat While
π repeats a given code block as long as a condition is π. This also means that if the condition is never π the code block will never be executed. The syntax is:
repeat-while βΆ π condition block
For example, this program will infinitely print βIt goes on and on and onβ.
π π π
π π€It goes on and on and onπ€βοΈ
π
Due to the ease of use of the π statement π is only used very seldom.
ππ ππ Branch Speed
The decorators ππ (slow) and ππ (fast) allow you to specify to the compiler which branches in an βͺοΈ will be slow or fast. This can enable better optimizations in performance sensitive code.
The example below shows an if statement whose only branch has been marked as slow:
βͺοΈ size π count πππ
size β¬
οΈβοΈ 2
β£οΈ π
π data sizeβοΈβοΈElementβοΈ
π
π