Mutability

From Adam Colton
Revision as of 14:31, 18 September 2019 by Adamcolton (talk | contribs)
Jump to navigation Jump to search

Mutability will be explicit.

Variables are mutable or immutable.

Mono Mute

One idea I like but I'm not sure about it's feasibility; there can only ever be one mutable reference. For linear stack calls, this is easy. If we disallow any form of global mutable, it's also easy.

The only complicating factor is concurrency. If I have a mutable reference that I want to share with a concurrent call, then there's two references.

Case Study

How would you add a memoization wrapper? Specifically, to a concurrent Fibonacci call.

Ternary

Mutability really has 3 variants

  • Writeable : value is mutable and this reference is allowed to mutate it
  • Mutable : value is mutable but this reference can't mutate it.
  • Immutable : No writeable reference exists.

Expressions

It might be good to disallow mutable calls in expressions.

 if a.Foo() || a.Bar() {...}

If a.Bar can mutate a and || is evaluated lazily, this is a good spot for bugs to hide.

Functions

Functions can infer if they are immutable. A function that does not call any functions with side effects or mutate it's arguments is immutable.

This is one of the places where using the same bytecode for the compiler and runtime will be advantageous. If a function is immutable and it's arguments are known at compile time, the compiler can run the function and replace the return value.