thp/doc-generator/markdown/en/docs/latest/functions/lambdas.md
2023-03-28 19:18:06 -05:00

952 B

Anonymous functions/Lambdas

Subject to change

An anonymous consists of the fn keyword, parameters and body.

fn (x, y) {
    // Body
    x + y
}

The following types are optional, if we can be able to infer them:

  • The types of the parameters
  • The return type

If used, they would look like this:

fn (Int x, Int y) -> Int {
    // Body
    x + y
}

Lambdas

If an anonymous function doesn't have parameters, the fn keyword may be omitted. This is called a lambda.

{ doSomething() }

// The above lambda is equivalent to:
fn () {
    doSomething()
}

Inferred arguments

If the arguments of the lambda don't need names, the following syntax can be used.

{ $1 + $2 }

Inside a short lambda you can use $1, $2, $3, etc. to refer to the parameters of the lambda.

So the following are the same:

{ $1 + $2 }

// The same as:
fn (x, y) {
    x + y
}