2 ms·
You can have both mutability and immutability in the same language. Example from Ruby: $ irb 2.4.1 :001 > a = [1, 2, 3] => [1, 2, 3] 2.4.1 :0
by pmontra 9y ago
You can have both mutability and immutability in the same language. Example from Ruby:
$ irb
2.4.1 :001 > a = [1, 2, 3]
=> [1, 2, 3]
2.4.1 :002 > a.map {|x| x + 1}
=> [2, 3, 4]
2.4.1 :003 > a
=> [1, 2, 3]
2.4.1 :004 > a.map! {|x| x + 1}
=> [2, 3, 4]
2.4.1 :005 > a
=> [2, 3, 4]
2.4.1 :006 > a.freeze # make a immutable
=> [2, 3, 4]
2.4.1 :007 > a.map! {|x| x + 1}
RuntimeError: can't modify frozen Array
Ruby is fundamentally a language based on mutability, but it shows that it should be possible to have both ways. Questions:
1) Is really there a language which is fully agnostic about mutability and immutability?
2) If not, it's maybe because designers have strong opinions about this matter? Actually IMHO designing a fully agnostic language would demonstrate strong opinions too.
- moosingin3space 9y agoI'd argue most languages are agnostic about (im)mutability. It's libraries that aren't.
- pmontra 9y agoMost but some don't. This fails in Erlang X = X + 1. because even variables are immutable. This is ok in Elixir even if it kind of fakes it [1] x = x + 1 and it's perfectly ok in Ruby and most other languages. [1] http://blog.plataformatec.com.br/2016/01/comparing-elixir-and-erlang-variables/ http://blog.plataformatec.com.br/2016/01/comparing-elixir-an...