On this page:
def
let
.
8.12

6.2 Definitions🔗ℹ

definition

def lhs_bind = rhs_expr

 

definition

def lhs_bind:

  body

  ...

 

lhs_bind

 = 

bind

 | 

values(bind, ...)

 | 

(bind, ...)

Binds the identifiers of binds to the values of rhs_expr or the body sequence. The body itself can include definitions, and it normally ends with an expression to provide the result values.

A bind can be just an identifier or id_name, or it can be constructed with a binding operator, such as a pattern form or :: for annotations. The number of result values must match the number of binds. Static information is gathered from rhs_expr or body and propagated to lhs_bind as described in Rules for Static Information).

An identifier is bound in the expr space, and most binding operators also create bindings in the expr space.

When def is used with =, then rhs_expr must not contain any immediate = terms (although = can appear nested in blocks, parentheses, etc.). When a def group both contains a = and ends in a block, the block is treated as part of an rhs_expr after the =.

> def pi = 3.14

> pi

3.14

> def pi:

    let tau = 6.28

    tau/2

> pi

3.14

> def [x, y, z] = [1+2, 3+4, 5+6]

> y

7

> def ns :: List = [1+2, 3+4, 5+6]

> ns

[3, 7, 11]

definition

let lhs_bind = rhs_expr

 

definition

let lhs_bind:

  body

  ...

Like def, but for bindings that become visible only after the let form within its definition context. The let form cannot be used in a top-level context outside of a module or local block. The let can be used with the same name multiple times in a module or block, but the same name cannot be defined with both def and let within a module or block.

> block:

    let v = 1

    fun get_v(): v

    let v = v+1

    [get_v(), v]

[1, 2]

binding operator

id_name . id

The . operator works somewhat like a binding operator that works only with identifiers, and it specifies a namespace-prefixed identifier to bind as an extension of an already-defined namespace. More precisely, . to join identifiers in a binding position is recognized literally as an operator, along the same lines as . used to reference a binding within a namespace or import.

See Namespaces for more information about extending namespaces.

> namespace geometry:

    export:

      pi

    def pi = 3.14

> def geometry.tau = 2 * geometry.pi

> geometry.tau

6.28