Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an object by reference and value in Julia?

Tags:

julia

I know that from here:

Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions. Function arguments themselves act as new variable bindings (new locations that can refer to values), but the values they refer to are identical to the passed values. Modifications to mutable values (such as Arrays) made within a function will be visible to the caller. This is the same behavior found in Scheme, most Lisps, Python, Ruby and Perl, among other dynamic languages.

Given this, it's clear to me that to pass by reference, all you need to do is have a mutable type that you pass into a function and edit.

My question then becomes, how can I clearly distinguish between pass by value and pass by reference? Does anyone have an example that shows a function being called twice; once with pass by reference, and once with pass by value?

I saw this post which alludes to some similar ideas, but it did not fully answer my question.

like image 852
logankilpatrick Avatar asked Sep 28 '19 21:09

logankilpatrick


1 Answers

In Julia, functions always have pass-by-sharing argument-passing behavior:

https://docs.julialang.org/en/v1/manual/functions/

This argument-passing convention is also used in most general purpose dynamic programming languages, including various Lisps, Python, Perl and Ruby. A good and useful description can be found here:

https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing

In short, pass-by-sharing works like pass-by-reference but you cannot change which value a binding in the calling scope refers to by reassigning to an argument in the function being called—if you reassign an argument, the binding in the caller is unchanged. This means that in general you cannot use functions to change bindings, such as for example to swap to variables. (Macros can, however, modify bindings in the caller.) In particular, if a variable in the caller refers to an immutable value like an integer or a floating-point number, its value cannot be changed by a function call since which object the variable refers to cannot be changed by a function call and the value itself cannot be modified as it is immutable.

If you want to have something like R or Matlab pass by value behavior, you need to explicitly create a copy of the argument before modifying it. This is precisely what R and Matlab do when an argument is passed in a modified and an external reference to the argument remains. In Julia it must be done explicitly by the programmer rather than being done automatically by the system. A downside is that the system can sometimes know that no copy is required (no external references remain) when the programmer cannot generally know this. That ability, however, is deeply tied with the reference counting garbage collections technique, which is not used by Julia due to performance considerations.

By convention, functions which mutate the contents of an argument have a ! postfix (e.g., sort v/s sort!).

like image 152
Przemyslaw Szufel Avatar answered Sep 24 '22 00:09

Przemyslaw Szufel