-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
mention typeof functions in types docs #41855
Conversation
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. |
@simeonschaub: good point, concrete suggestions to the PR would be appreciated. |
Suggest to add another note on types of anonymous functions:
A question: Is this documenting Julia itself or the implementation (compiler, runtime, etc) ? |
@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 |
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
false | |
false | |
julia> addy(1).y | |
1 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
@tpapp, the added content is to emphasize the differences between the type of a top level function and the type of an anonymous one. |
Co-authored-by: Simeon Schaub <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
Co-authored-by: Simeon Schaub <[email protected]>
Add === comparison suggested by @rfourquet.
@rfourquet, @simeonschaub: thanks for your constructive comments. I think this is now ready, a review would be appreciated. |
doc/src/manual/types.md
Outdated
|
||
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
typeof
functions in the types section of the manual, with an exampletypeof(foo)
is merely how it is printedtypeof
anonymous functions, add an example which is not a singleton