call by value

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
19 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

call by value

Martin Koroudjiev
Hi,

How will we have to interpret this citation from "Erlang Programming" by
Francesco Cesarini and Simon Thompson (page 30):

"All calls with variables in Erlang are call by value: all arguments to
a function call are evaluated before the body of the function is
evaluated. The concept of call by reference does not exist, ..."

I know that variables are not copied when making a function call and are
copied when sending messages to processes. So why do the authors say all
calls are call by value? Aren't the function calls actually call by
reference?

Regards,

Martin
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Matthew Sackman-2
On Thu, May 19, 2011 at 06:36:02PM +0300, Martin Dimitrov wrote:
> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?

Well I think there is a difference of interpretation of the terminology
based on whether you're in C world or functional world. To me, they're
more talking about laziness: Erlang is strict and as such expressions in
arguments to function calls are reduced to terms before the function is
invoked. I'm not sure that's exactly the same as call by value, but
given the idempotency of data structures in functional languages
generally, it's not clear that a C-style interpretation of call-by-value
or call-by-reference has any meaning for functional languages.

Matthew
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Ulf Wiger
In reply to this post by Martin Koroudjiev

You should ignore the issue of copying. Essentially, Erlang can be viewed as using copy-on-write semantics.

The key is whether the expression is evaluated or not (it is) before the function is called, and also whether the called function can modify the input parameters (it can't)*.

* Unless you use unholy functions like hipe_bifs:bytearray_update/3, which of course you don't. ;-)

BR,
Ulf W

On 19 May 2011, at 17:36, Martin Dimitrov wrote:

> Hi,
>
> How will we have to interpret this citation from "Erlang Programming" by
> Francesco Cesarini and Simon Thompson (page 30):
>
> "All calls with variables in Erlang are call by value: all arguments to
> a function call are evaluated before the body of the function is
> evaluated. The concept of call by reference does not exist, ..."
>
> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?
>
> Regards,
>
> Martin
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Daniel Dormont
In reply to this post by Martin Koroudjiev
In my experience with other languages, the difference between call-by-value and call-by-reference is not generally understood to depend on whether the data in memory is physically copied or not. That's really an implementation detail. Rather, the difference is whether the argument "itself" is passed as opposed to its value.

Personally, I consider the distinction meaningless in languages like Erlang (or Haskell, or even, say, SQL) that lack mutable variables.

dan

On May 19, 2011, at 11:36 AM, Martin Dimitrov wrote:

> Hi,
>
> How will we have to interpret this citation from "Erlang Programming" by
> Francesco Cesarini and Simon Thompson (page 30):
>
> "All calls with variables in Erlang are call by value: all arguments to
> a function call are evaluated before the body of the function is
> evaluated. The concept of call by reference does not exist, ..."
>
> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?
>
> Regards,
>
> Martin
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Martin Koroudjiev
On 5/19/2011 6:43 PM, Daniel Dormont wrote:
> In my experience with other languages, the difference between call-by-value and call-by-reference is not generally understood to depend on whether the data in memory is physically copied or not. That's really an implementation detail.
On first read I can't think of an example where the implementation is
not actually copying the value.
> Rather, the difference is whether the argument "itself" is passed as opposed to its value.
>
Would then in Erlang be more appropriate to say that the argument
"itself" is being passed, thus saying call-by-reference?
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Paulo Sérgio Almeida
In reply to this post by Ulf Wiger
On 5/19/11 4:41 PM, Ulf Wiger wrote:
> You should ignore the issue of copying. Essentially, Erlang can be viewed as using copy-on-write semantics.
>
> The key is whether the expression is evaluated or not (it is) before the function is called, and also whether the called function can modify the input parameters (it can't)*.
>
> * Unless you use unholy functions like hipe_bifs:bytearray_update/3, which of course you don't. ;-)
Unless you need to implement huge bloom filters with good performance. ;-)

Regards,
Paulo

> BR,
> Ulf W
>
> On 19 May 2011, at 17:36, Martin Dimitrov wrote:
>
>> Hi,
>>
>> How will we have to interpret this citation from "Erlang Programming" by
>> Francesco Cesarini and Simon Thompson (page 30):
>>
>> "All calls with variables in Erlang are call by value: all arguments to
>> a function call are evaluated before the body of the function is
>> evaluated. The concept of call by reference does not exist, ..."
>>
>> I know that variables are not copied when making a function call and are
>> copied when sending messages to processes. So why do the authors say all
>> calls are call by value? Aren't the function calls actually call by
>> reference?
>>
>> Regards,
>>
>> Martin
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
> Ulf Wiger, CTO, Erlang Solutions, Ltd.
> http://erlang-solutions.com
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Robert Raschke
In reply to this post by Martin Koroudjiev

On Thu, May 19, 2011 at 4:50 PM, Martin Dimitrov <[hidden email]> wrote:
On 5/19/2011 6:43 PM, Daniel Dormont wrote:
> In my experience with other languages, the difference between call-by-value and call-by-reference is not generally understood to depend on whether the data in memory is physically copied or not. That's really an implementation detail.
On first read I can't think of an example where the implementation is
not actually copying the value.
> Rather, the difference is whether the argument "itself" is passed as opposed to its value.
>
Would then in Erlang be more appropriate to say that the argument
"itself" is being passed, thus saying call-by-reference?


You have to remember that in Erlang there are only values. You don't have references. What you have is names for values, but that's all it is, a name for a value, just like you use the word "one" to denote the value of the numeral 1.

The great thing about having only values, is that you don't have to make copies in memory, because there's no way to destroy or update them.

You can emulate references by using state carrying data structures. Some assembly required.

Robby

PS And yes, you can delve into deeper aspects, where the "value" metaphor breaks down a bit. But usually you need to have a reasonably good reason for doing that.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Matthew Sackman-2
On Thu, May 19, 2011 at 05:14:49PM +0100, Robert Raschke wrote:
> You have to remember that in Erlang there are only values. You don't have
> references. What you have is names for values, but that's all it is, a name
> for a value, just like you use the word "one" to denote the value of the
> numeral 1.

Though interestingly, you can think of a Pid as a reference to a
process. Likewise Refs (clue is in the name ;)) and Ports.

Matthew
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Ulf Wiger
In reply to this post by Paulo Sérgio Almeida

On 19 May 2011, at 17:59, Paulo Sérgio Almeida wrote:

> On 5/19/11 4:41 PM, Ulf Wiger wrote:
>> You should ignore the issue of copying. Essentially, Erlang can be viewed as using copy-on-write semantics.
>>
>> The key is whether the expression is evaluated or not (it is) before the function is called, and also whether the called function can modify the input parameters (it can't)*.
>>
>> * Unless you use unholy functions like hipe_bifs:bytearray_update/3, which of course you don't. ;-)
> Unless you need to implement huge bloom filters with good performance. ;-)

Yeah, well, I was bitten by exactly that last week, as I prepared a code kata based on a bloom filter.

I had a bloom filter implementation that used hipe_bifs:bitarray_update/3 for performance and simplicity, but since I was messing with those, I used them also in the code calling on the bloom filter (which was to find all words of a given length).

The fun started when I then made a QLC generator out of the CPS-style word finder. It worked, but instead of returning all words of length N, it returned a list of several instances of the SAME word.

It took me a little while to realise that, as I was using destructive update when permuting the "character vector", the QLC iterator kept mutating the same binary the whole time, and simply returned a pointer to that one binary for each word found.

Mutation is evil…

BR,
Ulf W

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Jesper Louis Andersen-2
In reply to this post by Martin Koroudjiev
On Thu, May 19, 2011 at 17:36, Martin Dimitrov <[hidden email]> wrote:

> "All calls with variables in Erlang are call by value: all arguments to
> a function call are evaluated before the body of the function is
> evaluated. The concept of call by reference does not exist, ..."

I'll reply even though others have replied and add a bit more flesh.
Suppose we have a function:

f(X) ->
  X + 7.

and we want to call it like this:

f(3 + 5),

now there are two ways the reduction can proceed. Either we first
evaluate the expression and then make the call,

f(3 + 5) ==> f(8) ==> 8 + 7 ==> 15

which is the call-by-value evaluation strategy, or we do it by
substituting in the expression first,

f(3 + 5) ==> (3 + 5) + 7 ==> 8 + 7 ==> 15

which is the call-by-reference evaluation strategy. Erlang is a
Call-by-value (or an eager/strict) language. Clean and Haskell are
good examples of languages that use a variant the call-by-reference
strategy named call-by-need.

You may be confusing the evaluation strategy with whether parameters
are passed by value or by reference. This is an important notion in an
imperative language like Java, C, C++, Go, etc, but in functional
languages pass-by-value is the only thing you can do. Remember though
that in a functional language values are persistent and thus
immutable. Hence, we can get away with reusing the same in-memory
structure over and over again until we change it. And when we do
change it, we can exploit to share as much as possible from the old
structure. We define

L = [1],

with an in-memory representation like this:

{P1} ==> []

where P1 is a pointer to a cell containing the value 1 and ===> is a
pointer and [] is a special value signifying the end-of-list. Now, if
cons it two times:

L2 = [2 | L],
L3 = [3 | L],

We get two lists like this:

{P2} ====\
            {P1} ==> []
{P3} ====/

My ASCII-art hopefully showing how the L2 and L3 lists share the tail.
Immutability like this means we can just pass a reference and that is
as good as having made a copy of the whole data structure. It can be
exploited by your programs giving you some possibilities which are
quite funny to think about. For instance:

X = 3,
L = {X, X}
L2 = {L, L},
L3 = {L2, L2},
L4 = {L3, L3},
L5 = {L4, L4}.

L5 is built in linear time and contains 32 X cells. That is, we just
built an exponentially large data structure in linear time. Also, all
of it references the same X in memory. Yet if we ask for the sum of
the tree-like structure, then we would get the correct answer of 32 *
3.

So in conclusion: everything is passed by value, but where possible,
Erlang will pass by reference due to the immutable behaviour of the
Erlang heap. So the whole question of the passing style
(value/refence) is specific the implementation and not needed to
understand how the language evaluates (everything is pass-by-value).

> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?

Yes, when you send a message to another process, Erlang has to move
the structure you are sending to the other process. This is because
processes have separate heaps, so you must somehow move the data from
one heap to the other and copying is the quickest/simplest way to do
that. Storing and retrieving data from/to ETS has the same copying.

--
J.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Jesper Louis Andersen-2
On Thu, May 19, 2011 at 18:23, Jesper Louis Andersen
<[hidden email]> wrote:
>
> f(3 + 5) ==> (3 + 5) + 7 ==> 8 + 7 ==> 15
>
> which is the call-by-reference evaluation strategy. Erlang is a
> Call-by-value (or an eager/strict) language. Clean and Haskell are
> good examples of languages that use a variant the call-by-reference
> strategy named call-by-need.
>

Ok, I was wrong here. Haskell and Clean are call-by-need, but I was
thinking of call-by-name and not call by reference. My bad, see

http://en.wikipedia.org/wiki/Evaluation_strategy

for the full information about it.


--
J.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Martin Koroudjiev
In reply to this post by Jesper Louis Andersen-2
Thanks for the explanation. Now I am glad I asked that question.

I had always thought of call-by-value as the actual act of copying the
value which now I understand it might be or might not be as in the case
with Erlang.

Regards,

Martin

On 5/19/2011 7:23 PM, Jesper Louis Andersen wrote:

> On Thu, May 19, 2011 at 17:36, Martin Dimitrov <[hidden email]> wrote:
>
>> "All calls with variables in Erlang are call by value: all arguments to
>> a function call are evaluated before the body of the function is
>> evaluated. The concept of call by reference does not exist, ..."
> I'll reply even though others have replied and add a bit more flesh.
> Suppose we have a function:
>
> f(X) ->
>   X + 7.
>
> and we want to call it like this:
>
> f(3 + 5),
>
> now there are two ways the reduction can proceed. Either we first
> evaluate the expression and then make the call,
>
> f(3 + 5) ==> f(8) ==> 8 + 7 ==> 15
>
> which is the call-by-value evaluation strategy, or we do it by
> substituting in the expression first,
>
> f(3 + 5) ==> (3 + 5) + 7 ==> 8 + 7 ==> 15
>
> which is the call-by-reference evaluation strategy. Erlang is a
> Call-by-value (or an eager/strict) language. Clean and Haskell are
> good examples of languages that use a variant the call-by-reference
> strategy named call-by-need.
>
> You may be confusing the evaluation strategy with whether parameters
> are passed by value or by reference. This is an important notion in an
> imperative language like Java, C, C++, Go, etc, but in functional
> languages pass-by-value is the only thing you can do. Remember though
> that in a functional language values are persistent and thus
> immutable. Hence, we can get away with reusing the same in-memory
> structure over and over again until we change it. And when we do
> change it, we can exploit to share as much as possible from the old
> structure. We define
>
> L = [1],
>
> with an in-memory representation like this:
>
> {P1} ==> []
>
> where P1 is a pointer to a cell containing the value 1 and ===> is a
> pointer and [] is a special value signifying the end-of-list. Now, if
> cons it two times:
>
> L2 = [2 | L],
> L3 = [3 | L],
>
> We get two lists like this:
>
> {P2} ====\
>             {P1} ==> []
> {P3} ====/
>
> My ASCII-art hopefully showing how the L2 and L3 lists share the tail.
> Immutability like this means we can just pass a reference and that is
> as good as having made a copy of the whole data structure. It can be
> exploited by your programs giving you some possibilities which are
> quite funny to think about. For instance:
>
> X = 3,
> L = {X, X}
> L2 = {L, L},
> L3 = {L2, L2},
> L4 = {L3, L3},
> L5 = {L4, L4}.
>
> L5 is built in linear time and contains 32 X cells. That is, we just
> built an exponentially large data structure in linear time. Also, all
> of it references the same X in memory. Yet if we ask for the sum of
> the tree-like structure, then we would get the correct answer of 32 *
> 3.
>
> So in conclusion: everything is passed by value, but where possible,
> Erlang will pass by reference due to the immutable behaviour of the
> Erlang heap. So the whole question of the passing style
> (value/refence) is specific the implementation and not needed to
> understand how the language evaluates (everything is pass-by-value).
>
>> I know that variables are not copied when making a function call and are
>> copied when sending messages to processes. So why do the authors say all
>> calls are call by value? Aren't the function calls actually call by
>> reference?
> Yes, when you send a message to another process, Erlang has to move
> the structure you are sending to the other process. This is because
> processes have separate heaps, so you must somehow move the data from
> one heap to the other and copying is the quickest/simplest way to do
> that. Storing and retrieving data from/to ETS has the same copying.
>

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Richard A. O'Keefe-2
In reply to this post by Martin Koroudjiev

On 20/05/2011, at 3:36 AM, Martin Dimitrov wrote:

> Hi,
>
> How will we have to interpret this citation from "Erlang Programming" by
> Francesco Cesarini and Simon Thompson (page 30):
>
> "All calls with variables in Erlang are call by value: all arguments to
> a function call are evaluated before the body of the function is
> evaluated. The concept of call by reference does not exist, ..."
>
> I know that variables are not copied when making a function call and are
> copied when sending messages to processes. So why do the authors say all
> calls are call by value? Aren't the function calls actually call by
> reference?

No.

Here's call by reference:

        subroutine swap(x, y)
           real x, y, t
           t = x
           x = y
           y = t
        end

        program main
           real x, y
           x = 1.0
           y = 2.0
           call swap(x, y)
           print x, y
        end

outputs 2, 1.  Call by reference is where when you pass a
variable to a procedure, it gets a reference to the variable,
not the current value of the variable, and it can use that
to change what the value of the variable is.

It isn't that Erlang doesn't *copy* variables, but rather
that it doesn't *have* variables in that sense.

Now an Erlang *value* may be implemented as a pointer to a
record in memory, but that implementation is NOT visible to
an Erlang program.  Erlang values could be implemented as
strings "under the hood" and only the cost model would change,
not what you can do.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Richard A. O'Keefe-2
In reply to this post by Matthew Sackman-2

On 20/05/2011, at 3:40 AM, Matthew Sackman wrote:
> Well I think there is a difference of interpretation of the terminology
> based on whether you're in C world or functional world. To me, they're
> more talking about laziness: Erlang is strict and as such expressions in
> arguments to function calls are reduced to terms before the function is
> invoked. I'm not sure that's exactly the same as call by value,

"strict" argument evaluation is precisely call by value.
The terminology used in the original quotation was that of the C++ world
(and others), where
        void foo(int &x) {...}
        ... foo(y) ...
passes the *address* of the mutable variable y to foo
so that foo may change it, whereas
        void bar(int x) {...}
        ... foo(y) ...
passes the *contents* of the mutable value y to foo.


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Richard A. O'Keefe-2
In reply to this post by Jesper Louis Andersen-2

On 20/05/2011, at 4:23 AM, Jesper Louis Andersen wrote:

> On Thu, May 19, 2011 at 17:36, Martin Dimitrov <[hidden email]> wrote:
>
>> "All calls with variables in Erlang are call by value: all arguments to
>> a function call are evaluated before the body of the function is
>> evaluated. The concept of call by reference does not exist, ..."
>
> I'll reply even though others have replied and add a bit more flesh.
> Suppose we have a function:
>
> f(X) ->
>  X + 7.
>
> and we want to call it like this:
>
> f(3 + 5),
>
> now there are two ways the reduction can proceed. Either we first
> evaluate the expression and then make the call,
>
> f(3 + 5) ==> f(8) ==> 8 + 7 ==> 15
>
> which is the call-by-value evaluation strategy, or we do it by
> substituting in the expression first,
>
> f(3 + 5) ==> (3 + 5) + 7 ==> 8 + 7 ==> 15

That is *NOT* call by reference.
It is call by *NAME*, which is quite difference.
(The Algol 60 committee intended to specify call by reference,
but used the "copy rule" to define it, which to their dismay
specified call by name instead.)

Pascal 'var' parameters are call by reference, not by name.\

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Richard A. O'Keefe-2
In reply to this post by Martin Koroudjiev

On 20/05/2011, at 7:07 AM, Martin Dimitrov wrote:

> Thanks for the explanation. Now I am glad I asked that question.
>
> I had always thought of call-by-value as the actual act of copying the
> value which now I understand it might be or might not be as in the case
> with Erlang.

It's precisely the same in Java, Javascript, C#, Eiffel, Lisp, you name it
(except for Pascal).

PASSING a value is a constant time operation;
for small integers this might be equivalent to copying,
but for "values" that are (references to) linked structures,
no copying is done.

The way Java talks about it, if you pass an array to a method,
the *object reference* is the value that is passed, and this happens
to point to a shared mutable object which is *not* copied.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Martin Koroudjiev

> but for "values" that are (references to) linked structures,
> no copying is done.
>
If you are talking about Java, isn't this copy-by-reference?
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Richard A. O'Keefe-2

On 20/05/2011, at 11:59 PM, Martin Dimitrov wrote:

>
>> but for "values" that are (references to) linked structures,
>> no copying is done.
>>
> If you are talking about Java, isn't this copy-by-reference?

I've never seen the term "copy-by-reference" before,
so I can't tell.  It certainly isn't PASS by reference.

void incr(int &x) {
    x++;
}

int main(void) {
    int b = 2;
    incr(b);
    cout << b << endl;
}

in C++ (a) uses pass by reference, and (b) prints 3.
You cannot do that in Java.

Java has pass by value and ONLY pass by value.
Some of those values are (perhaps confusingly)
called "object references" (in practice they are
almost always just plain pointers, something
Java is, again misleadingly, said not to have).,


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: call by value

Daniel Dormont
On Sun, May 22, 2011 at 9:33 PM, Richard O'Keefe <[hidden email]> wrote:

On 20/05/2011, at 11:59 PM, Martin Dimitrov wrote:

>
>> but for "values" that are (references to) linked structures,
>> no copying is done.
>>
> If you are talking about Java, isn't this copy-by-reference?

I've never seen the term "copy-by-reference" before,
so I can't tell.  It certainly isn't PASS by reference.

void incr(int &x) {
   x++;
}

int main(void) {
   int b = 2;
   incr(b);
   cout << b << endl;
}

in C++ (a) uses pass by reference, and (b) prints 3.
You cannot do that in Java.

Java has pass by value and ONLY pass by value.
Some of those values are (perhaps confusingly)
called "object references" (in practice they are
almost always just plain pointers, something
Java is, again misleadingly, said not to have).,


Correct. Remember Java came from C++, and in the early days most people expressing interest in Java were C++ programmers. The "no pointers" phrasing was meant to emphasize the fact that Java's pointers were opaque, not constructible and you could not perform arithmetic on them.

Relatedly when I first read about Refs in Erlang, I assumed they were similar to the ones in ML or Haskell, that is, immutable references to mutable data elements. That took correcting.

dan



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Loading...