Emojicode Documentation 0.8
Guides Language Reference Package Index

Callables

Emojicode supports a type called callables. Callables are like methods (or more generally functions) that can be passed like any other object.

Type

The type of a callable is denoted using this syntax:

callable-type ⟢ πŸ‡ [type-list] [return-type] πŸ‰
type-list ⟢ type | type type-list

Each of the types provided before the return type stands for one argument of that type. The return type is optional. If no return type is specified the callable does not return a value.

Examples:

πŸ‡πŸ”’βž‘οΈπŸ”‘πŸ‰  πŸ’­ Takes an integer argument and returns a string
πŸ‡βž‘οΈπŸ”£πŸ‰  πŸ’­ Takes no arguments and returns a symbol
πŸ‡πŸ‰  πŸ’­ Takes no arguments and does not return a value.

Calling a Callable

Callables are called using this syntax.

callable-call ⟢ ⁉️ expression [arguments] mood

The first expression must be a callable.

Example of calling a callable:

πŸ’­ greet is of type πŸ‡πŸ”‘πŸ‰
⁉️ greet πŸ”€BobπŸ”€β—οΈ

Closure

Closures are blocks of code that are not immediately executed but remember the context in which they were created, i.e. the variables and the context of the method, and can be passed around. The type of a closure is, of course, a callable.

A closure is created by a block of code that appears when an expression is expected. This means that every code block that is not part of an β†ͺ️, πŸ™…, or πŸ™…β†ͺ️ πŸ”‚ or πŸ” statement or a method or initializer declaration, is a closure.

In contrast to a normal code block, a closure can define arguments and a return type similar to a method.

Formally, its syntax is:

closure ⟢ πŸ‡ [parameters] [return-type] statements πŸ‰

We can define a very simple closure that does not capture any context like this:

πŸ‡ name πŸ”‘
  πŸ˜€ πŸͺπŸ”€It is a plesaure to welcome the honorable πŸ”€ nameπŸͺ ❗️
πŸ‰ ➑️ greet

⁉️ greet πŸ”€LindaπŸ”€β—οΈ

Running this code would print:

It is a plesaure to welcome the honorable Linda

Capturing Variables

Let’s take a look at a more advanced use of a closure:

πŸ‡ 🍀 πŸ‡
  πŸ‡β—οΈ πŸ™‹ name πŸ”‘ ➑️ πŸ‡πŸ”’βž‘οΈπŸ”‘πŸ‰ πŸ‡
    ↩️ πŸ‡ hour πŸ”’ ➑️ πŸ”‘
      β†ͺ️ hour πŸ™Œ 12 πŸ‡
        ↩️ πŸͺ πŸ”€Have a good lunch, πŸ”€ nameπŸͺ
      πŸ‰
      ↩️ πŸͺ πŸ”€Hello, πŸ”€ nameπŸͺ
    πŸ‰
  πŸ‰
πŸ‰

Here we’ve got a type method that returns a closure. The closure actually closes over the name variable here and copies it value so that it can be used when the closure is called later.

Now, if we call that type method we’ll get a closure:

πŸ™‹πŸ‡πŸ€ πŸ”€VioletπŸ”€β—οΈ ➑️ violetGreeter

We can call the closure with an argument for hour

πŸ˜€ ⁉️violetGreeter 14❗️❗️
πŸ˜€ ⁉️violetGreeter 12❗️❗

and will get this:

Hello, Violet
Have a good lunch, Violet
Caution

Closures copy the closured local variables and make them available as constant variable in the closure. Instance variables are not copied and change when modified inside a closure.

← Previous Next Up: β€œPackages” β†’
Something not quite right? Improve this page