3 ms·
The problem with the last code snippet is that it looks like mutating the original value, and that would mislead newcomers to the language.
by Ndymium 8y ago
The problem with the last code snippet is that it looks like mutating the original value, and that would mislead newcomers to the language.
- pmontra 8y agoYes, that's possible. However this is an example from Ruby, which is very mutable but has some immutable features in its standard library. 2.3.0 :001 > s = "abc" => "abc" 2.3.0 :002 > s.gsub("b", "B") => "aBc" 2.3.0 :003 > s => "abc" 2.3.0 :004 > s.gsub!("b", "B") => "aBc" 2.3.0 :005 > s => "aBc" I guess that if an immutable language shows some mutable syntax we can expect it to be a short form. Actually Elixir has at least a mutable short form: iex(1)> s = "abc" "abc" iex(2)> String.replace(s, "b", "B") "aBc" iex(3)> s "abc" The next line would be an error in Erlang and other languages because we can't mutate an already assigned variable there, but Elixir allows rebinding iex(4)> s = String.replace(s, "b", "B") "aBc" iex(5)> s "aBc"