News for hackers. Stuff that matters.
Re:Deep-copying in JavaScript by jerf on Thursday January 25, 2018 @02:45PM (#16233927)
Nowadays, everything is pass-by-value. The only question is whether a reference or a pointer is one of the things that can be passed by value, or if everything passed is always a copy.

An exception to pass-by-value would be Forth; in the case of stack-based languages there really isn't a "pass" going on at all. But everything copies something for every non-inlined function call nowadays. Another instance where it's less obvious what is going on is a truly immutable language like Erlang or Haskell, where the "value" is quite likely still being passed via a pointer, but that's only relevant from the point of view of considering garbage collection behavior.

This makes the old "pass-by-value vs pass-by-reference" terminology vestigial to the modern software engineer, which is why the only place you'll encounter it is in school courses. A better way of understanding the modern issues is to ask what powers a passed-in value has and just ignore the old question.

Re:Deep-copying in JavaScript by kmill on Thursday January 25, 2018 @03:28PM (#16234320)
Off the top of my head, here are languages in use that have a feature for pass-by-reference semantics: C#, Ada, C++ (though in this language you can get ahold of the reference as a value if you want).

Of course, to implement pass by reference, you tend to have to pass pointers around, but the point is that the language gives the illusion that the identifiers within a function's scope are locations specified by the caller.

I guess a takeaway is that in designing a language, one has to consider the relationship between an identifier, the mutable cell associated with the identifier, and the value stored in that cell. It seem that in common languages the identifier refers to the cell, but if the identifier is used in a value context, it evaluates to the value in the cell. Languages with pass-by-reference semantics let you make your own & function, so to speak.

(A weird example in C of this relationship is that an identifier for an array refers to a cell that contains the whole array, but, if you use the identifier in a value context, it becomes a pointer to the first element. One aspect of it not actually being a pointer is that you cannot replace its value with the pointer to another array.)

Anyway, an everything-must-be-a-first-class-object hard-liner would insist that pass-by-reference is the way of the past. But, sometimes it is not economical to get everything to be a first-class object. For instance, you might decide it is not worth trying to create a pointer type for referring to members of a packed struct, but it is not too hard to make a pass-by-reference feature for this (via copying).

Re:Deep-copying in JavaScript by Roboprog on Thursday January 25, 2018 @05:02PM (#16235109)
Pascal “var” parameters. Pascal is rarely used any more, but I suppose it’s no more obscure than Ada.
Re:Deep-copying in JavaScript by geoelectric on Thursday January 25, 2018 @04:21PM (#16234782)
The key feature of pass-by-reference is whether the callee can replace the value bound to the caller's argument (not mutate it).

In other words, pseudocoded:

    x=1
    changeMe(x)
    write(x) // 2
With that in mind, it's clearer that JS is pass-by-value with object reference values (or pass-by-sharing, though I never hear that in practice) as the caller remains bound to the passed object, but a C++ reference argument is an actual pass-by-reference of the original binding. So's an argument passed to a Pascal/Delphi out or variable parameter, and I'm sure other examples exist as well.

So I guess it depends on what you mean by "nowadays" but I'd still consider C++ a modern language within "everything".

Re:Deep-copying in JavaScript by quicknir on Friday January 26, 2018 @11:50AM (#16240638)
You can never actually replace what the reference is bound to in C++, you can only call the assignment operator. The real distinction here is that in languages like Java and python, assignment is fundamentally different from mutation. In C++ there is no distinction, assignment is just a form of mutation.
Re:Deep-copying in JavaScript by amptorn on Thursday January 25, 2018 @03:04PM (#16234081)
Did you know? Perl is pass-by-reference.

    sub mutate {
      $_[0] = "blue";
    }
    
    my $color = "red";
    mutate($color);
    print($color); # "blue"
Re:Deep-copying in JavaScript by dragonwriter on Thursday January 25, 2018 @04:30PM (#16234859)
> Nowadays, everything is pass-by-value.

Even viewing sharing as a subtype of value, that's not true; call by need exists in modern languages (e.g., Haskell), and, in fact, classic call-by-reference is available in lots of languages, though not the exclusive model in any (and not the primary model in any current popular language I can think of.)

Re:Deep-copying in JavaScript by paulddraper on Thursday January 25, 2018 @03:40PM (#16234438)
> Nowadays, everything is pass-by-value.

Except for obscure, seldom-used languages like C++ and C#.

If you can write

    int a;
    ParseInt("123", a);
    Print(a); // print 123
your language has pass by reference.
Re:Deep-copying in JavaScript by pjmlp on Thursday January 25, 2018 @03:46PM (#16234495)
I would add other unknown languages like Ada, D, VB.NET, Delphi, FreePascal, Rust.
Re:Deep-copying in JavaScript by tomjakubowski on Thursday January 25, 2018 @05:59PM (#16235543)
Rust's references are ordinary values. Moreover, the GP's example wouldn't work in Rust; you'd need to pass a value of type &mut i32 to ParseInt to mutate the value.
Re:Deep-copying in JavaScript by shkkmo on Thursday January 25, 2018 @02:54PM (#16234013)
What are you talking about?

PHP has pass-by-reference: http://php.net/manual/en/language.references.pass.php

Re:Deep-copying in JavaScript by jerf on Thursday January 25, 2018 @02:57PM (#16234032)
And the references are passed by value. There are two places you can find in RAM that will contain that reference, which once you penetrate through the abstractions and wrappers, will be a pointer; that second location is the reference that was passed by value. A reference is basically a pointer that the language prevents you from doing pointer arithmetic on, and in many languages such as PHP, is simply automatically dereferenced for you.

The reason why I brought up languages like Forth is that there really did used to be languages that did not actually "pass" things by value. Forth had a true pass-by-reference, in which there is no second copy of anything, neither the value, nor a pointer to the value. Now it's a dead distinction, in the sense that pass-by-value and pass-by-reference used to be distinguishing, because everything is copying something. Which is why the distinction is sort of confusing to try to apply to modern languages, and produces lots of heat but no light; it's dead, inapplicable to the modern language landscape.

The proper question to be asking is what permissions/privileges are passed by a function's parameter, whether you're allowed to modify it and have the modifications visible to the caller, whether you're not allowed to modify it at all (immutable languages, for instance), or whether there's an entire ownership system around what the passed-in value represents (Rust), not whether or not something was copied when the function call was made. Trying to answer this question in terms of "pass-by-X" doesn't help, if for no other reason than pass-by-X implies there's only two possibilities, and I outlined three classes of possibilities above, each of which can have their own further nuances. Is Rust pass-by-reference or pass-by-value? Well, the question is invalid, in either the original or the modern mutation of the meaning.

Re:Deep-copying in JavaScript by zamalek on Thursday January 25, 2018 @03:47PM (#16234508)
> There are two places you can find in RAM that will contain that reference

That is an implementation detail completely depends on the VM. There is one very simple exception to your example: call inlining. Pass-by-reference is a behavior specification, not an implementation specification.

> Rust

If Rust emitted purely pass-by-value machine code, zero-cost-abstractions would be impossible in the language.

Re:Deep-copying in JavaScript by zzzcpan on Thursday January 25, 2018 @03:57PM (#16234594)
It's mostly about explicit vs. implicit. If a reference is implicit - it's pass-by-reference.
Re:Deep-copying in JavaScript by zamalek on Thursday January 25, 2018 @04:13PM (#16234725)
Not according to the comment that I am replying to:

> A reference is basically a pointer that the language prevents you from doing pointer arithmetic on, and in many languages such as PHP, is simply automatically dereferenced for you.

Also, the implicit is usually pass-by-value.

    function foo(a) { a++; }
    function bar(ref a) { a++; }
    b = 1;
    foo(b); // Pass by value, implicit.
    assert(b == 1);
    bar(ref b); // Pass by reference.
    assert(b == 2);
Heap vs. stack values are not the same thing as pass-by-reference and pass-by-value.
Re:Deep-copying in JavaScript by kmill on Thursday January 25, 2018 @03:47PM (#16234500)
> Is Rust pass-by-reference or pass-by-value? Well, the question is invalid, in either the original or the modern mutation of the meaning.

What do you mean? Maybe you mean "invalid" as in "no one should ask this anymore," but in case you mean "invalid" as in "is an apple an orange?" then

- It's strictly pass-by-value in the classic meaning. Sure, because of immutability there's the obvious optimization the compiler can do which is, under the hood, pass a pointer to a caller's value, but you could also implement everything by copying without the programmer noticing a difference (except for how long the program takes to run).

- It's strictly not pass-by-reference in the classic meaning. The "references" are a type of value. Parameters never are equivalent to the passed-in variable.

Whether or not it's a useful distinction to make is another story. References-as-values seems to be the emerging dominant paradigm for language design, but I sort of wish there were more languages that let you say "this variable is the fifth element of this array."

> if for no other reason than pass-by-X implies there's only two possibilities

I guess because people have only ever heard of by value and by reference, but there are a whole bunch more: by name, by copy-restore, by sharing.

Re:Deep-copying in JavaScript by shkkmo on Thursday January 25, 2018 @03:12PM (#16234161)
pass-by-value, pass-by-reference and pass-reference-by-value are all distinct.

PHP does all three, pass-by-value (simple values), pass-reference-by-value (objects and arrays), pass-by-reference (when using & prefix)

The terms still have important distinctions in modern languages as they are functionally different.

Re:Deep-copying in JavaScript by dragonwriter on Thursday January 25, 2018 @04:03PM (#16234649)
> Trying to answer this question in terms of "pass-by-X" doesn't help, if for no other reason than pass-by-X implies there's only two possibilities

No, it doesn't. Heck, when I learned pass- (or call-)-by-X there were three main values of X mentioned (and implicitly a near-universal number of possible alternatives): value, reference, and name.

There are actually several more recognized values; the Wikipedia article on Evaluation Strategy has a reasonably good list:

https://en.m.wikipedia.org/wiki/Evaluation_strategy