5 ms·
A random thought about the code samples. What do we gain from having to write point<~(y=value) instead of other languages' point.y = value ? I beli
by pmontra 8y ago
A random thought about the code samples. What do we gain from having to write
point<~(y=value)
instead of other languages'
point.y = value
?
I believe that syntax should be designed to make things simple for developers, not for the designers of languages or for compilers.
- emmanueloga_ 8y agoI don't know anything about the language but the first form suggests immutability (return a new point with the y property = to value), the second looks like mutating a struct
- garmaine 8y agoI don't see anything intrinsically different about the two. If anything the established notation of mathematics for hundreds of years is that '=' implies immutability.
- titanomachy 8y agoThe second example suggests to me that `point` already exists, and we're mutating it by changing the value of its `y` property.
- ken 8y ago<~ looks like an expression for a bulk update of an immutable record. "point.y = value" is typically a statement for a single update of a mutable record. I'm not sure how you'd extend the latter to support the features of the former. The meaning is completely different. Different things should look different.
- auggierose 8y agoI think this is nicely solved in Swift.
- ken 8y agoHow? I don't know any way to do a bulk update of an immutable structure in Swift, apart from constructing an entirely new structure and passing all values (not just changed ones) to its initializer.
- auggierose 8y agoYou can just declare all members of the structure as vars. The structure will still be immutable.
- pmontra 8y agoI 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
- threeseed 8y agoOr to use Scala's syntax which is more clear to me: point.copy(y=value)
- SuddsMcDuff 8y agoI really like F#'s approach to this let point2 = { point1 with y = value } https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/copy-and-update-record-expressions https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe... EDIT This is inherited from OCaml - https://v1.realworldocaml.org/v1/en/html/records.html#functional-updates https://v1.realworldocaml.org/v1/en/html/records.html#functi...
- augustk 8y ago"Choosing the equal sign to denote assignment is one notoriously bad example that goes back to Fortran in 1957 and has been copied blindly by armies of language designers since. This bad idea overthrows a century-old tradition to let = denote a comparison for equality, a predicate that is either true or false. But Fortran made this symbol mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand, a variable, is to be made equal to the right operand, an expression. Thus, x = y does not mean the same thing as y = x. Algol corrected this mistake with a simple solution: Let assignment be denoted by :=." --Niklaus Wirth (Good Ideas,Through the Looking Glass)
- mimixco 8y agoLisp, the second oldest language after Fortran, didn't make this mistake. In Lisp, "=" isn't an operator at all. Assignment is (setf x y) and comparison is (eq x y) [and several variants of eq].
- kazinator 8y agoCommon Lisp and related languages have = as a function: it is a numeric comparison. Like under equalp, (= 1 1.0) yields true. You might think it's superfluous to equalp, but has some virtues of its own, like supporting variadic arguments (= 1 1 1 1) and rejecting non-numeric arguments. eq is often the wrong one to use: two instances of the same number can fail to be eq.
- SlowRobotAhead 8y agoEquals is a verb AND a adjective though. So I disagree it's notoriously bad to use it as assignment. This is a situation solved entirely with = (make these equals) and == (are these equals?). >Thus, x = y does not mean the same thing as y = x. Unless it does, or should. The specfic argument you're arguing here is on that is just a simple syntax. You don't like = for assignment, but I do. You like = for comparison, but I like ==... To be fair === is dumb. You're arguing that point<~(y=value) makes sense, and I'm still not seeing it.
- 8y ago
- Gene_Parmesan 8y agoThis is a bulk record update, according to what I can find in the docs. It's an atomic operation -- meaning, if instead of updating one field, you updated thirty fields, all would appear to take place as one operation. Obviously someone please correct me if I'm wrong.