fadion / aria

Expressive, noiseless, interpreted, toy programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Immutability and variable modification

aymanhs opened this issue · comments

First of all, nice work. Love the clean syntax.

I do like the idea of immutable values, but you are creating immutable variables which IMHO is very limiting. Basically that makes each let create a constant. You need immutable values, not immutable variables.

I now cannot change x at all? Not even increment it? How can you make a program to get sum of numbers? You can't create a new variable for each iteration of the summing loop.

Glad you like it and thanks for the feedback.

What do you mean, practically, by immutable values and what difference would that make in Aria's implementation?

Maybe I'm used somewhat to functional languages that treat everything as immutable, so to me, it makes total sense. Having a let statement the only point of change, makes it very easy to reason about a program's path of execution.

In Go for example, where everything is mutable (except strings, but that's another story), you would increment every element of an array with a for loop:

numbers := []int{1, 2, 3, 4}
for i, v := range numbers {
    numbers[i] = v + 1
}

In Aria, you would have something very similar, but as the for in loop is an expression that actually returns a value, there's no need to mutate:

let numbers = [1, 2, 3, 4]
let plus_one = for v in numbers
  v + 1
end

The difference in 4 lines of code is unnoticeable, but on thousands, knowing that numbers will always have the same value no matter what, in my opinion, increases clarity and sanity.

Serious languages that provide some form of immutability have two major advantages over a toy one like Aria:

  1. Transforming an immutable value in a new variable will not always create a new copy. They optimise them to reuse parts of the original values, hence using less memory.
  2. They offer extensive functionality from the standard library, especially for enumerables, so you'll never need a mutable i. I plan to enhance this side of Aria.

These are just my ideas which I'm totally willing to discuss.

For your "sum of numbers", yes, right now you can't do that with immutable values. That's why higher-order functions like reduce come in handy and hopefully, I'll be implementing it (together with map, filter, etc.) in the near future.

Thanks for the reply. To explain my point in other words, basically Aria's let is equivalent to most other languages const. And I see that many languages that promote immutability, e.g. Scala, have support for both const and var. I guess it will be clearer with the use of higher order functions.

Languages like Scala and Swift take the mixed approach: you better use immutable values but if you need it, there's mutability too. I'm personally fond of Elixir/Erlang's approach: immutability everywhere and a fantastic standard library.

I'm in the process of implementing higher order functions. Actually map and filter are finished, so will get back to this discussion with some examples.

With the addition of more library functions, which will be added periodically, I believe immutability is much less of an issue. Agree to close this?

PS: The standard library docs include examples too, if you're curious for map and reduce.

Finally took your advice and implemented mutable values via var declarations. I'm trying to implement the standard library functions with Aria itself, and immutability makes it practically impossible.