Skip to content

mention typeof functions in types docs #41855

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

Merged
merged 9 commits into from
Aug 17, 2021

Conversation

tpapp
Copy link
Contributor

@tpapp tpapp commented Aug 10, 2021

  • mention typeof functions in the types section of the manual, with an example
  • explain that typeof(foo) is merely how it is printed
  • also add examples for typeof anonymous functions, add an example which is not a singleton

@tpapp tpapp added the docs This change adds or pertains to documentation label Aug 10, 2021
@simeonschaub
Copy link
Member

simeonschaub commented Aug 10, 2021

I think we need to be careful here not to conflate "function", "function defined at top-level", "closure" and "anonymous function". I think we typically use the term function to denote ones defined at top-level as well as closures. I also don't think anonymous functions are equivalent to closures, since we do have named closures and anonymous functions can also be used at top-level, where they won't close over any variables.

@tpapp
Copy link
Contributor Author

tpapp commented Aug 11, 2021

@simeonschaub: good point, concrete suggestions to the PR would be appreciated.

@foldl
Copy link

foldl commented Aug 11, 2021

Suggest to add another note on types of anonymous functions:

Types of two anonymous functions may even be the same:

julia> fs = [(x) -> i + x for i = 0:1]
...
julia> typeof(fs[1]) === typeof(fs[2])
true

A question: Is this documenting Julia itself or the implementation (compiler, runtime, etc) ?

@tpapp
Copy link
Contributor Author

tpapp commented Aug 11, 2021

@foldl: yes, those types are the same, but this is not something I would encourage users to rely on, even if it is likely to remain so.

Types in Julia are mainly used for dispatch, so while it is idiomatic to rely on type of named toplevel functions, eg typeof(sin), one may not want to use these types in 99% of other cases. We should just document how anonymous function types print so that users are not surprised.

Copy link
Member

@simeonschaub simeonschaub left a comment

Choose a reason for hiding this comment

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

Some suggestions

addy (generic function with 1 method)

julia> Base.issingletontype(addy(1))
false
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
false
false
julia> addy(1).y
1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't closures having fields an implementation detail, something we would not necessarily want to expose in the user manual?

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't call that an implementation detail. It is a defining feature of closures.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't sound like a defining feature of closures, after all they were implemented differently in the past, and the field names could have been made unavailable through getproperty even with the current implementation. But I don't know whether that's now specified somewhere in the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's in the dev docs.

Incidentally, is it OK to link devdocs in the user manual?

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't sound like a defining feature of closures, after all they were implemented differently in the past, and the field names could have been made unavailable through getproperty even with the current implementation.

I actually didn't know that, thanks! I suspect that changing this now would be quite breaking, so we can probably document this somewhere, but we can leave it out of this PR for now.

Copy link
Member

Choose a reason for hiding this comment

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

Incidentally, is it OK to link devdocs in the user manual?

I didn't notice that at first. We should probably have something on closures n the regular docs as well, but for now, maybe we can just have the link point to anonymous functions instead, since in most cases they are actually closures as well.

Copy link
Member

Choose a reason for hiding this comment

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

It's in the dev docs.

Thanks. As the "dev docs" document Julia internals, I wouldn't link to the them from the user manual. To show the point here, you might insert in the sessions addy(1) === addy(2) # false, and/or maybe show the output of addy(1) (i.e. var"#3#4"{Int64}(1), which hints at the implementation detail).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I added that example, and now only refer to closures in passing.

@foldl
Copy link

foldl commented Aug 11, 2021

@tpapp, the added content is to emphasize the differences between the type of a top level function and the type of an anonymous one.

@tpapp
Copy link
Contributor Author

tpapp commented Aug 17, 2021

@rfourquet, @simeonschaub: thanks for your constructive comments. I think this is now ready, a review would be appreciated.


Types of functions defined at top-level are singletons. When necessary, you can compare them with [`===`](@ref).

[Anonymous functions](@id man-anonymous-functions) also have their own type, which is usually printed with names that end in `#<number>`. Names and types for functions defined at different locations are distinct, but not guaranteed to be printed the same way across sessions.
Copy link
Member

Choose a reason for hiding this comment

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

We can link to the anonymous function section, but I still believe we should use the word closure here.

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 made that change. The manual also uses "closure" liberally in other places so this should be OK.

@fredrikekre fredrikekre merged commit c70db59 into JuliaLang:master Aug 17, 2021
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
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