Skip to content

Conversation

@StefanKarpinski
Copy link
Member

@StefanKarpinski StefanKarpinski commented Mar 13, 2018

All uses of "by reference" in docs are technically incorrect. Several of the explanations of behavior around this were also wrong. This corrects them. Inspired by #26427.

closes #23127

@giordano
Copy link
Member

Perhaps you could add a link to https://en.m.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing? I think call by sharing is less known compared to the other strategies.

@StefanKarpinski
Copy link
Member Author

Good idea. Updated.

@jrklasen
Copy link
Contributor

closes #23127

* LLVM scalars and vectors are passed by value.
* LLVM aggregates (arrays and structs) are passed by reference.
* LLVM aggregates (arrays and structs) are [passed by
sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) (i.e. as pointers).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refers to the ABI, so I don't think the call-by-sharing terminology is really necessary or helpful.

underscore), and so to call a Fortran function via [`ccall`](@ref) you must pass
the mangled identifier corresponding to the rule followed by your Fortran
compiler. Also, when calling a Fortran function, all inputs must be passed as
pointers to heap-allocated values. This applies not only to arrays and other
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't have to be heap-allocated.

@StefanKarpinski StefanKarpinski force-pushed the sk/sharing branch 4 times, most recently from ff4c71a to 6fb38df Compare March 14, 2018 01:38
simply declared as `(Csize_t,)` without any `Ref` or `Ptr` necessary. (If the
wrapper was calling a Fortran function instead, the corresponding function input
signature should instead be `(Ref{Csize_t},)`, since Fortran variables are
passed by pointers.) Furthermore, `n` can be any type that is convertable to a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still say "by reference", since we're referring to Fortran here.

"GNU Fortran passes most arguments by reference, i.e. by passing a pointer to the data" (https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.html)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not since there's a different and conflicting meaning of the term even though Fortran did use it in a different way first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, Fortran actually does pass arguments by reference though, since it doesn't have pointers and the variables themselves are values, not bindings.

Copy link
Member Author

@StefanKarpinski StefanKarpinski Mar 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but from Julia's perspective nothing is "passed by reference" – mutable objects are passed by sharing (via pointers) to a function which cannot change any bindings in the caller but can mutate shared objects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's fine. But the text above is literally "... Fortran variables are passed by reference". We can reword it to clarify that the subject is Julia "..., since Fortran expects Julia to pass a pointer to the value." But in the above clause, the subject is Fortran. Another way to say it is that "..., since Fortran passes variables by reference, which is equivalent in Julia to passing a pointer"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be fine but saying that they are "passed by pointers" is also correct.

while this prevents accidental modification by callees of a value in the caller,
it makes avoiding unwanted copying of arrays difficult. In Julia, modifications
made to input arrays within a function will be visible in the parent function.
Unless a function ends with a `!` indicating that it mutates one or more of its
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternate wording? "By convention, a function name ending with a ! indicates that it will mutate or destroy the value of one or more of its arguments. Callees must make explicit copies to ensure that they don't modify inputs that they don't intent to change. Many non-mutating functions are implemented by calling a function of the same name with an added ! at the end on an explicit copy of the input, and returning that copy."

@StefanKarpinski StefanKarpinski merged commit 3fe38f8 into master Mar 14, 2018
@StefanKarpinski StefanKarpinski deleted the sk/sharing branch March 14, 2018 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Documentation: Misleading statement about passing behavior of immutables

7 participants