Language reference · section 23

23. Memo

Feature: Memo

  Scenario: @memo caches return values by argument
    Given a function with @memo
    When the function is called twice with the same primitive argument
    Then the second call returns the cached value

  Scenario: Primitive arguments are keyed by value
    Given the arguments number, string, boolean, null, or undefined
    Then the cache key is the value

  Scenario: Object and array arguments are keyed by reference
    Given two different objects with the same fields
    When each object is passed to a @memo function
    Then each object has its own cache entry

  Scenario: The cache has no eviction
    Given a @memo function
    Then the cache lives as long as the function

The compiler does not check that a @memo function has no side effects. The writer must make that true.

@memo
fn fib(n: int): int:
  if n < 2:
    return n

  return fib(n - 1) + fib(n - 2)