Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document return nothing in Functions man page (2nd PR) #33028

Merged
merged 3 commits into from Nov 15, 2019

Conversation

pierre-haessig
Copy link
Contributor

Hi,

This is my 2nd try to document the return nothing convention in the Functions manual. My first PR #27286 was 1 year old, so that some of my changes became superfluous (in particular, the documentation of return in base/docs/basedocs.jl now does mention nothing thanks to PR #30716).

Still, the Functions manual lacks the changes I proposed. Here a more compact PR, which answers @KristofferC feedback (e.g. I removed any reference to C's return;). The increased compactness should hopefully make it easier to merge!

I kept only one offtopic change: in the first PR, I was updating the link to Markdown syntax in doc/ README.md. In the meantime, it was changed, but it's again broken, so here is a one line fix which may not deserve a PR of its own.

+ fix broken link to Markdown syntax in doc README
### Returning nothing

Some functions are used only for their side effects, and do not need to return a value.
In these cases, the Julia convention is to return the value [`nothing`](@ref):
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This is slightly at odds with the convention to return the modified object as in

julia> a = rand(2); push!(a, 2.0)
3-element Array{Float64,1}:
 0.7926734563461015
 0.6590165997556594
 2.0 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. In fact I copy pastd this fragment from the FAQ. What do you thinkg of:

For functions which do not need to return a value, the Julia convention is to return the value nothing
...


```julia
function printx(x)
println("x = $x")
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Might be good to use an expression here that doesn't already evaluate to nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I understand you see this example as contrived because it would be equivalent without the return nothing line, is this correct? Indeed I hadn't thought of that. One way would be to write down this remark. As for another example, do you have any idea?

Instead of a display function (I guess that all display functions other than println will also return nothing), we could use a function which sets some global variable like

function setflag(a)
    global flag
    flag = a
    return nothing
end

However, this kind of function may more likely return the new value rather than nothing. Also, the global keyword is introduced two chapters later (if we consider the manual to be a sequential reading). What do you think?

@pierre-haessig
Copy link
Contributor Author

I've added a commit in response of both @KristofferC (functions with side effects) and @StefanKarpinski (contrived example) comments.

@pierre-haessig
Copy link
Contributor Author

There are some builbot errors, but it's not clear to me if it relates to the PR.

@kshyatt kshyatt added the domain:docs This change adds or pertains to documentation label Oct 15, 2019
@kshyatt
Copy link
Contributor

kshyatt commented Oct 15, 2019

In freebsd, it was Distributed which broke which should be unrelated.
In osx64, it was FileWatching which is also iirc a notorious problem, but should be unrelated.
Win64 timed out with no output.

Is this ready to go?

@pierre-haessig
Copy link
Contributor Author

Now that @JeffBezanson comment is adressed, I believe the PR may be merged (unless some builbot errors appear).

@JeffBezanson JeffBezanson merged commit 4d7701e into JuliaLang:master Nov 15, 2019
@pierre-haessig
Copy link
Contributor Author

Thanks! This truely closes #27286.

@pierre-haessig pierre-haessig deleted the return-nothing branch November 19, 2019 13:22
KristofferC pushed a commit that referenced this pull request Apr 11, 2020
* document return nothing in Functions man page

+ fix broken link to Markdown syntax in doc README
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants