Skip to content

Function return looses mutability #117

@delasy

Description

@delasy

You can easily omit mutable flag for a variable if you do this:

fn unmut (a: ref int) ref int {
  return a
}

main {
  a := 10
  mut aMut := unmut(ref a)
}

One way to solve it by making all ReturnStatement return immutable value that can't be modified, unless specified otherwise with Mutable generic:

fn unmut (mut a: ref int) Mutable<ref int> {
  return a
}

main {
  mut a := 10
  mut aMut := unmut(ref a)
}

Fail if trying to get mutable value out of immutable return:

fn unmut (a: ref int) Mutable<ref int> {
  return a // error: `a` is immutable
}

main {
  mut a := 10
  mut aMut := unmut(ref a)
}

Fail if trying to assign immutable variable to Mutable generic:

fn unmut2 (a: ref int) Mutable<ref int> {
  return a // error: `a` is immutable
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions