3 ms·
I expand a little my point. This is from Elixir, functional and immutable, which I've been working with for a couple of years iex(1)> point = %{x: 1, y: 2,
by pmontra 8y ago
I expand a little my point. This is from Elixir, functional and immutable, which I've been working with for a couple of years
iex(1)> point = %{x: 1, y: 2, z: 3}
%{x: 1, y: 2, z: 3}
Of course this doesn't work
iex(2)> point.x = 4
** (CompileError) iex:2: cannot invoke remote function point.x/0 inside match
This is how we do it, verbosely
iex(3)> Map.put(point, :x, 4)
%{x: 4, y: 2, z: 3}
Or with a syntax shortcut
iex(4)> %{point | x: 4}
%{x: 4, y: 2, z: 3}
A better syntax shortcut would be the usual
iex(nope)> point.x = 4
%{x: 4, y: 2, z: 3}
which everybody would understand no matter the language of origin. I imagine that it would require quite an overhaul of the internals of the compiler, but the compiler should bend to us, not us to the compiler. This is my point about obscure language syntaxes.
- Ndymium 8y agoThe 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"
- ken 8y agoI would not understand that. In every language I've used that supported "point.x = 4", the returned value (if there is one) is 4, not point. Bosque's update is more like CONS (or conj) than . = Your proposed syntax also doesn't have any obvious way to do a bulk update, and I'm not sure what syntax you're proposing for that. I maintain that it's more important for things to look like how they act, rather than look familiar -- especially if the familiar appearance does something functionally different -- https://stackoverflow.com/a/522168 https://stackoverflow.com/a/522168