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

Replace -> with :: where appropriate in docstrings #57012

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LilithHafner
Copy link
Member

In cases where a function is documented as

```
    function(arg::ArgT, arg2::Arg2T) -> RetT

...
```

I either switched -> to :: or switched RetT to ret_name::RetT.

From the recommendation and justification from @nsajko here: #56978 (comment) and applied throughout the repo.

@LilithHafner LilithHafner added the docs This change adds or pertains to documentation label Jan 10, 2025
Copy link
Contributor

@mcabbott mcabbott left a comment

Choose a reason for hiding this comment

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

At some point I made a branch switching to --> instead (but never made a PR). One of the goals was things like this, where what is returned seems more important than the type:

lmul!(a::Number, B::AbstractArray) --> B     # mutates & returns 2nd arg

eigvals!(A, B; sortby) --> values::Vector    # returns neither arg

I see the PR would make these eigvals!(A, B; sortby) -> values::Vector. Perhaps that seems awkwardly almost-code (same complaint as the present ->)? My idea was to make a smaller visual change, and move in the direction of clearly-not-syntax.

I also had these... which do parse as capturing :(s --> Bool) but at least --> has no definition, while s::Bool does mean something which isn't true:

@isdefined s --> Bool

 @__LINE__ --> Int

@@ -1443,7 +1443,7 @@ function _prepend!(a::Vector, ::IteratorSize, iter)
end

"""
resize!(a::Vector, n::Integer) -> Vector
resize!(a::Vector, n::Integer) -> a
Copy link
Contributor

Choose a reason for hiding this comment

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

or switched ... to ret_name::RetT.

Took a while to find an example of what exactly this meant.

If the reason to use :: is that this is valid syntax, does this want to use -> when it's not?

@@ -3,7 +3,7 @@
# Conversion/Promotion

"""
Date(dt::DateTime) -> Date
Date(dt::DateTime)::Date
Copy link
Contributor

Choose a reason for hiding this comment

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

Should cases like this be annotated at all, or is it just clutter?

The default assumption that SomeType(...) must construct a SomeType, exceptions (at there any?) need a giant warning. Annotating all the normal cases, if anything, seems to hint that there might be exceptions to watch out for.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree with you: Just clutter.

@@ -147,7 +147,7 @@ macro specialize(vars...)
end

"""
@isdefined s -> Bool
@isdefined s::Bool
Copy link
Contributor

@mcabbott mcabbott Jan 10, 2025

Choose a reason for hiding this comment

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

This parses as calling the macro on :(s::Bool), which isn't great. (The old one parsed a a function of course, but the point of the PR is to remove that.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! I intended to skip macros in this PR but accidentally included that. Should be fixed (probably just reverted/left alone)

@jmert
Copy link
Contributor

jmert commented Jan 10, 2025

At some point I made a branch switching to --> instead (but never made a PR). One of the goals was things like this, where what is returned seems more important than the type:

lmul!(a::Number, B::AbstractArray) --> B     # mutates & returns 2nd arg

eigvals!(A, B; sortby) --> values::Vector    # returns neither arg

What about using assignment syntax to name variables instead?

B = lmul!(a::Number, B::AbstractArray)

values = eigvals!(A, B; sortby)::Vector

maxval, index = findmax(A; dims)

@jishnub
Copy link
Contributor

jishnub commented Jan 11, 2025

Previous discussion on this at #22804

@LilithHafner
Copy link
Member Author

Reading the previous discussion and existing docstrings, I propose that :: is used to indicate return types and -> is used to indicate return values, following these patterns:

    f(x::T)::U
    f(x::T) -> y::U
    f(x::T) -> (y::U1, z::U2)
    f(x::T)::Tuple{U1, U2} # valid and reasonable, but I'd prefer the option above 
    @mac x -> y::U
    @mac -> y::U
    @mac::U # Not this, it's not valid syntax
    @mac -> U # Also not this, it's inconsistent with the forms above. Provide a variable name for the output.
    U(x::T) # Don't annotate the return type if it's obvious

@laborg
Copy link
Contributor

laborg commented Jan 11, 2025

I like the idea from @mbauman to use → instead of -> to distinguish it from anonymous functions. If this is used, I'd also prefer having it consistently, even if only the type is specified:

f(x::T) ⇥ # when nothing is returned
f(x::T) → ::U
f(x::T) → y::U
f(x::T) → (y::U1, z::U2)
f(x::T) → ::Tuple{U1, U2}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants