Language reference · section 10

10. Mutable parameters

Feature: Mutable parameters

  Scenario: A mut parameter changes the caller's binding
    Given the code:
      """
      fn bump(mut c):
        c = c + 1
        return c

      mut total = 1
      bump(total)
      """
    Then total is 2

  Scenario: The caller passes the name plainly
    Given a function with a mut parameter
    When the caller writes bump(total)
    Then the call needs no extra symbol

  Scenario: The argument must be a mutable binding
    Given a call that passes "t" to a mut parameter
    And t is not declared with mut
    Then the compiler reports an error "cannot pass immutable `t` to a `mut` parameter of `f`; declare it with `mut`"

  Scenario: The argument must be a plain name
    Given a call that passes an expression to a mut parameter
    Then the compiler reports an error "argument 1 of `f` is `mut`, so it must be the name of a mutable binding"

  Scenario: One name cannot be used twice in one call when it goes to a mut parameter
    Given a function whose first parameter is mut
    When the caller writes f(x, x) with a mutable x
    Then the compiler reports an error

  Scenario: A mut parameter is not allowed on a @worker function
    Given a @worker function with a mut parameter
    Then the compiler reports an error "`mut` parameters are not allowed on @worker functions"

  Scenario: A mut parameter is not allowed on a class method
    Given a class method with a mut parameter
    Then the compiler reports an error "`mut` parameters are not allowed on class methods"

  Scenario: An exported function cannot take a mut parameter
    Given an exported function with a mut parameter
    Then the compiler reports an error "`h` is exported, so it cannot take `mut` parameters; other modules cannot see them"