Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Higher-order Functions

Within a Wyn program, functions can be named, passed as arguments, and returned from other functions. Function values do not exist at runtime, however (see Program Structure), so the following restrictions apply to functions and to any record or tuple containing a function (a functional type):

  • Arrays of functions are not permitted.
  • A function cannot be returned from an if expression.
  • A loop parameter cannot be a function.

See also In-place Updates for details on how consumption interacts with higher-order functions.

Function Arity and Partial Application

Wyn functions are not curried by default. Every function has a fixed arity (number of arguments) and must be called with exactly that many arguments. Partial application is not allowed.

def add(x: i32, y: i32) i32 = x + y

-- Valid: fully applied
def result = add(1, 2)

-- INVALID: partial application
def add_one = add(1)  -- Error: function requires 2 arguments

This restriction applies uniformly to:

  • Top-level function definitions
  • Anonymous functions
  • Built-in functions
  • Functions passed as higher-order arguments

Explicit Currying with Placeholder Syntax

When a partially applied function is needed, use explicit placeholder syntax with $:

def add(x: i32, y: i32, z: i32) i32 = x + y + z

-- Create a 2-arity function that calls `add` with the middle arg
-- fixed.
def add_with_5 = $add(_, 5, _)        -- Produces (i32, i32) -> i32

-- Create a 1-arity function with two of the three args fixed.
def add_one = $add(_, 1, 0)           -- Produces i32 -> i32

-- Usage
def result = add_one(5)               -- Returns 6

The $func(args...) syntax:

  • _ marks placeholder positions that become parameters of the new function.
  • Non-placeholder arguments are captured at the definition site.
  • The resulting function has arity equal to the number of _ placeholders.
  • The resulting function is itself non-curried (it requires all placeholders to be filled at once).