1

I am referring to various discussions on the differences and nuances in the semantics of pass-by-value, pass-by-reference, e.g. in SO or in the Wikipedia article on the subject.

My question is why do these articles and conversations always discuss pass-by-value, pass-by-reference, etc. solely in the context of parameter passing and function calls and not in the context of assignments?

E.g. assuming the following:

A a1 = new A();
A a2 = a1;

... why is it taken for granted that the assigned object is not copied by value?

I am not asking why it would be a questionable language design decision to make the semantics of the assignment copy the object "by value" - I am asking whether and under what terminology language specifications explicitly spell this out or under what heading language design theory treats such decisions.

  • 1
    Take a look at en.wikipedia.org/wiki/Reference_type – Thomas Junk Feb 13 '15 at 18:10
  • 1
    My guess is that from a programming language theory perspective you'd model assignment as a function (that either returns nothing or the old value, depending on the language). – Doval Feb 13 '15 at 18:10
  • I always thought that assignments should be at least semantically "by value"... Or latter i thought they should be mostly disallowed, in favor of bindings. – AK_ Feb 13 '15 at 19:46
1

My question is why do these articles and conversations always discuss pass-by-value, pass-by-reference, etc. solely in the context of parameter passing and function calls and not in the context of assignments?

As Doval mentions in the comments, the primary reason is that assignment is often modeled as a function call (and can thus be ignored).

why is it taken for granted that the assigned object is not copied by value?

You mean like in C, C++ and a pile of other languages? These days, the most common reason is that copying non-trivial objects is not an atomic operation. Since it's not an atomic operation, it makes it a lot more difficult to make concurrent programs safely, which is already plenty difficult.

Also, because most programmers often don't want value semantics for their objects.

I am asking whether and under what terminology language specifications explicitly spell this out or under what heading language design theory treats such decisions.

The common terms are reference/value types and reference/value semantics (as mentioned in the comments).

  • 2
    Isn't the issue the slicing problem? You can't allocate a fixed amount of space for an object whose type you don't know until runtime. I don't think the atomicity is the issue; copying simple longs and doubles isn't atomic in Java unless you force the issue with volatile. – Doval Feb 13 '15 at 19:15
  • @Doval - ah yes, that is also an important problem. It's been so long since I've dealt with C++... – Telastyn Feb 13 '15 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.