From Julia's documentation:
Julia function arguments follow a convention sometimes called “pass-by-sharing”...
Does this mean that changing a mutable objects inside the function will also change the object in the caller scope?
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?
Am I right that adding !
at the end of function is just a convention, but has no any semantical meaning in compilation?
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)?
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
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.
Yes you are right.
Use deepcopy()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With