8000 Mutability design (`mut`) · Issue #112 · PistonDevelopers/dyon · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Mutability design (mut) #112
Closed
Closed
@bvssvni

Description

@bvssvni

Dyon uses mut to declare mutability. This appears in front of a function argument declaration or when calling a function that mutates the argument.

  • foo(mut a) mutates the argument a
  • foo(mut a) has function name foo(mut)
  • foo(mut a, b) has function name foo(mut,_)
  • foo(a, b) has function name foo

Local variables are mutable.

Mutability is not part of a type, but added to the function name inside parentheses, e.g.(mut,_). When adding an external function, the mutability information must be added as part of the function name.

Mutability propagates for arguments:

fn foo(mut a) { ... }

// `bar` requires `mut a` because it calls `foo(mut a)`.
fn bar(mut a) { foo(mut a) }

Multiple functions can use the same name if they have different mutability information. This reduces the need for "get", "set" and "mut" in function name:

fn title(window: 'return) -> { ... }

fn title(mut window, val) { ... }

t := title(window)
title(mut window, "hello world!")

This is designed for:

  • Explicitly declare when a function mutates a variable
  • Improve readability and maintenance
  • Allow a function name to be reused for different mutability patterns

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0