-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add @pure docstring and example #24817
Changes from 3 commits
76128ef
d36e196
75c08e7
53f4feb
8bf1561
5320afe
4a1034a
15a9c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,57 @@ macro noinline(ex) | |
esc(isa(ex, Expr) ? pushmeta!(ex, :noinline) : ex) | ||
end | ||
|
||
""" | ||
@pure | ||
|
||
Annotates a function to ease type inference by strictly specifying the | ||
output is completely determined by the input. | ||
|
||
Note: Misuse can lead to regressions. | ||
|
||
Do not use if the function involved: | ||
|
||
- Involves globals, pointers | ||
- Recurses | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What’s wrong with recursion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It came up during a Discourse post and @mbauman mentioned the restriction. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typejoin is pure and does recursion - seems to be doing OK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Will ammend. |
||
- Does not return exactly (`===`) the same result for the same input | ||
- Gets its methods extended after it is called | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, any. |
||
### Example | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example isn't wrong, but it will almost certainly be made obsolete by #24362 |
||
|
||
```julia-repl | ||
|
||
julia> struct Discrete{apply_map,scale_by_time} end | ||
|
||
julia> Discrete(;apply_map=false,scale_by_time=false) = Discrete{apply_map,scale_by_time}() | ||
Discrete | ||
|
||
julia> @code_warntype Discrete() | ||
Variables: | ||
#self# <optimized out> | ||
|
||
Body: | ||
begin | ||
return ((Core.apply_type)(Main.Discrete, false, false)::Type{Discrete{_,_}} where _ where _)()::Discrete{_,_} where _ where _ | ||
end::Discrete{_,_} where _ where _ | ||
|
||
julia> struct Discrete2{apply_map,scale_by_time} end | ||
|
||
julia> Base.@pure Discrete2(;apply_map=false,scale_by_time=false) = Discrete{apply_map,scale_by_time}() | ||
Discrete | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete me (duplicate code) |
||
|
||
julia> Base.@pure Discrete2(;apply_map=false,scale_by_time=false) = Discrete{apply_map,scale_by_time}() | ||
Discrete2 (generic function with 1 method) | ||
|
||
julia> @code_warntype Discrete2() | ||
Variables: | ||
#self# <optimized out> | ||
|
||
Body: | ||
begin | ||
return $(QuoteNode(Discrete{false,false}())) | ||
end::Discrete{false,false} | ||
``` | ||
""" | ||
macro pure(ex) | ||
esc(isa(ex, Expr) ? pushmeta!(ex, :pure) : ex) | ||
end | ||
|
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 don’t know what regression means. This should just say “Usage can easily lead to whole program corruption or crashes and should be avoided.”
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.
Did you mean to say "Usage" rather than "Misuse"? 😂