English

More on Classes

Initializers every subclass must provide (🔑)

If you put 🔑 on an initializer, every subclass must also provide that initializer. In the subclass, you write it with 🔑 too.

Emojicode
🐇 🐾 🍇
  🖍🆕 name 🔡
  🔑 🆕 🍼 name 🔡 🍇🍉
  ❗️ 🏷 ➡️ 🔡 🍇
    ↩️ name
  🍉
🍉

🐇 🐕 🐾 🍇
  🔑 🆕 name 🔡 🍇
    ⤴️🆕 name❗️
  🍉
🍉

🏁 🍇
  😀 🏷 🆕🐕 🔤Buddy🔤❗️❗️❗️
🍉
Output
Buddy

Methods that cannot be overridden (🔏)

If you put 🔏 on a method, subclasses can no longer override it.

Example of a compile error
🐇 🐾 🍇
  🆕 🍇🍉
  🔏❗️ 🗣 🍇
    😀 🔤…🔤❗️
  🍉
🍉

🐇 🐕 🐾 🍇
  ✒️ ❗️ 🗣 🍇
    😀 🔤Woof🔤❗️
  🍉
🍉

🏁 🍇
  🗣 🆕🐕❗️❗️
🍉

Visibility (🔓, 🔒, 🔐)

Write one of these before a method to decide where it can be called from.

Syntax Can be called from
🔓 (or nothing) anywhere
🔐 inside the class and its subclasses
🔒 only inside the class itself
Emojicode
🐇 🐾 🍇
  🆕 🍇🍉
  🔐❗️ 💓 ➡️ 🔡 🍇
    ↩️ 🔤Thump thump🔤
  🍉
🍉

🐇 🐕 🐾 🍇
  ❗️ 🎧 🍇
    😀 💓👇❗️❗️
  🍉
🍉

🏁 🍇
  🎧 🆕🐕❗️❗️
🍉
Output
Thump thump

Code that runs on cleanup (♻️)

♻️ 🍇 … 🍉 runs when an instance is no longer used and gets cleaned up (a destructor).

Emojicode
🐇 🎈 🍇
  🖍🆕 color 🔡
  🆕 🍼 color 🔡 🍇
    😀 🔤Blew up the 🧲color🧲 balloon🔤❗️
  🍉
  ♻️ 🍇
    😀 🔤The 🧲color🧲 balloon deflated🔤❗️
  🍉
🍉

🏁 🍇
  🆕🎈 🔤red🔤❗️ ➡️ red
  😀 🔤---🔤❗️
🍉
Output
Blew up the red balloon
---
The red balloon deflated

Emojicode manages memory with reference counting, and cleans an instance up as soon as its last reference goes away (Memory and References).

Generics on a single method

If you write 🐚Name Constraint🍆 after a method name, that method alone can take a type to use.

Emojicode
🐇 🧰 🍇
  🐇❗️ 👯 🐚T ⚪️🍆 value T count 🔢 ➡️ 🍨🐚T🍆 🍇
    🆕🍨🐚T🍆❗️ ➡️ 🖍🆕 list
    🔂 i 🆕⏩ 0 count❗️ 🍇
      🐻 list value❗️
    🍉
    ↩️ list
  🍉
🍉

🏁 🍇
  👯🐇🧰 🔤hi🔤 3❗️ ➡️ greetings
  😀 🆕🔡 greetings 🔤 🔤❗️❗️
  👯🐇🧰 7 2❗️ ➡️ sevens
  😀 🔡 📏 sevens❓❗️❗️
🍉
Output
hi hi hi
2

You don't have to write the type; it is inferred from the arguments.