Emojicode Documentation 1.0 beta 2
Guides Language Reference Package Index

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.

  1. ๐Ÿ”ฒ, โฌ›, ๐Ÿ”บ, โ‰๏ธ, ๐Ÿบ
  2. ๐Ÿšฎ, โž—, โœ–๏ธ
  3. โž–, โž•
  4. ๐Ÿ‘ˆ, ๐Ÿ‘‰
  5. โ—€๏ธ, โ–ถ๏ธ, โ—€๏ธ ๐Ÿ™Œ, โ–ถ๏ธ ๐Ÿ™Œ
  6. ๐Ÿ™Œ, ๐Ÿ˜œ
  7. โญ•๏ธ
  8. โŒ
  9. ๐Ÿ’ข
  10. ๐Ÿค
  11. ๐Ÿ‘

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.

โ† Previous Next Up: โ€œOptionalsโ€ โ†’
Something not quite right? Improve this page