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
Operators
Emojicode defines a set of operators.
binary-operator โถ โ | โ | โ | โ๏ธ | ๐ | ๐ค | โ๏ธ | โถ๏ธ | โญ๏ธ | ๐ข |
binary-operator โถ โ | ๐ | ๐ | ๐ฎ | ๐ | ๐ | โ๏ธ ๐ | โถ๏ธ ๐
Binary operators perform an operation on two values. For example, โ is an operator that is defined for the ๐ข type and adds up two values:
23 โ 11
operator-expression โถ expression binary-operator expression
Grouping
Consider the following example:
3 โ 2 โ๏ธ 2
Anybody with a reasonable understanding of maths will rightly expect the result to be 7 because 2 โ๏ธ 2
will be evaluated first.
However, what if we wanted 3 โ 2
to be evaluated first and add 2 to the result of that? Thisis what grouping is for.
Grouping allows you specify that the result of an operation is to be evaluated without regard to any operators before or after it.
group โถ ๐ค expression ๐ค
To achieve what we want, we can rewrite our code to
๐ค 3 โ 2 ๐ค โ๏ธ 2
and the result will be 10.
Operator Precedence
We have seen before that Emojicode knows that it must evaluate the โ๏ธ operation before the โ operation. This knowledge is called operator precedence.
The order in which operators are evaluated is clearly defined. Operators at top are evaluated first. Operators with equal precedence are evaluated from left to right.
- ๐ฒ, โฌ, ๐บ, โ๏ธ, ๐บ
- ๐ฎ, โ, โ๏ธ
- โ, โ
- ๐, ๐
- โ๏ธ, โถ๏ธ, โ๏ธ ๐, โถ๏ธ ๐
- ๐, ๐
- โญ๏ธ
- โ
- ๐ข
- ๐ค
- ๐
Short-Circuiting with ๐ค and ๐
The logical and operator ๐ค and the logical or operator ๐ are short-circuited. This means that ๐ค will only evaluate its right-hand side if the left was true. ๐, on the opposite, only evaluates the right-hand side if the left was false.
Due to this special behavior ๐ค and ๐ cannot be defined for any other type than ๐.
Defining Operations for Custom Types
You can also define operators for custom types. An operator can be defined similar to a method. This is an example from the s packageโs ๐ type:
โ b ๐ โก๏ธ ๐ ๐
count โ ๐bโ๏ธ โก๏ธ new_count
๐ญ ...
๐
The difference to a normal method declaration is simply that instead of an mood (โ๏ธ or โ) an operator appears. Furthermore, no name is specified.
Identity Check
๐ can be used to determine whether two objects references point to the same object in memory.
This isnโt an equality check: Two objects might represents the same value but they are still two different object not sharing the same memory location. To determine equality use ๐ค if available.
๐ returns true if the result of both expression are references to the same memory location. To avoid confusion it cannot be customly defined.