Mutability: Difference between revisions
Adamcolton (talk | contribs) Created page with "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 ..." |
Adamcolton (talk | contribs) No edit summary |
||
| Line 17: | Line 17: | ||
if a.Foo() || a.Bar() {...} | if a.Foo() || a.Bar() {...} | ||
If a.Bar can mutate a and || is evaluated lazily, this is a good spot for bugs to hide. | 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. | |||
Revision as of 17:39, 17 September 2019
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.
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.