Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia argument passing behavior

From Julia's documentation:

Julia function arguments follow a convention sometimes called “pass-by-sharing”...

  1. Does this mean that changing a mutable objects inside the function will also change the object in the caller scope?

  2. But if object is immutable then changing it inside the function will not affect the object in the caller scope? Is it in this case any different from passing by value?

  3. Am I right that adding ! at the end of function is just a convention, but has no any semantical meaning in compilation?

  4. What would be the best way to pass the value of the mutable object of the user-defined composite type? I tried using copy(), but got the error saying that the copy() is not defined for the my custom type. I guess I need to extend the copy() for my custom type. Where I can find the definitions of copy() for other types (would like to use them as a reference when writing extention)?

like image 791
aberdysh Avatar asked Oct 18 '22 18:10

aberdysh


1 Answers

  1. Does this mean that changing a mutable objects inside the function will also change the object in the caller scope?

The short answer is Yes. But keep that: "changing an object" is a wide concept, is it assigned a new memory location? If you reallocate a local variable in function scope, surely the sharing string with caller, will be cut. --> read more about mutation and assignment

  1. But if object is immutable then changing it inside the function will not affect the object in the caller scope? Is it in this case any different from passing by value?

That is because an immutable is not mutable so the only way to change it is by assigning a new memory location, and by this way the sharing string will be cut.

  1. Am I right that adding ! at the end of function is just a convention, but has no any semantical meaning in compilation?

Yes you are right.

  1. What .... ?

Use deepcopy() instead.

like image 91
Reza Afzalan Avatar answered Oct 21 '22 16:10

Reza Afzalan