-
-
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
added @macroexpand
#18660
Merged
Merged
added @macroexpand
#18660
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
988f7a1
added @macroexpand
jw3126 bff375b
Update expr.jl
jw3126 0b2c3ca
added @macroexpand to rst
jw3126 2dfe407
fix
jw3126 691052d
fixup: improving docu and removing test/expr.jl
jw3126 ebe9b53
fixup
jw3126 c66c096
Update expr.jl
jw3126 f8157dc
Update choosetests.jl
jw3126 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1052,6 +1052,7 @@ export | |
expand, | ||
gensym, | ||
macroexpand, | ||
@macroexpand, | ||
parse, | ||
|
||
# help and reflection | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,42 @@ Takes the expression `x` and returns an equivalent expression with all macros re | |
""" | ||
macroexpand(x::ANY) = ccall(:jl_macroexpand, Any, (Any,), x) | ||
|
||
""" | ||
@macroexpand | ||
|
||
Return equivalent expression with all macros removed (expanded). | ||
|
||
There is a subtle difference between `@macroexpand` and `macroexpand` in that expansion takes place in | ||
different contexts. This is best seen in the following example: | ||
|
||
```jldoctest | ||
julia> module M | ||
macro m() | ||
1 | ||
end | ||
function f() | ||
(@macroexpand(@m), macroexpand(:(@m))) | ||
end | ||
end | ||
M | ||
|
||
julia> macro m() | ||
2 | ||
end | ||
@m (macro with 1 method) | ||
|
||
julia> M.f() | ||
(1,2) | ||
``` | ||
With @macroexpand the expression expands where @macroexpand appears in the code (module M). | ||
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.
|
||
With macroexpand the expressions expands in the current module where the code was finally called. | ||
Note that when calling macroexpand or @macroexpand directly from the REPL, both of these contexts coincide, hence there is no difference. | ||
""" | ||
macro macroexpand(code) | ||
code_expanded = macroexpand(code) | ||
QuoteNode(code_expanded) | ||
end | ||
|
||
## misc syntax ## | ||
|
||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should be documented that the expansion happens in the module where the
@macroexpand
macro is expanded (i.e. the same module if the expression is used directly) and not the current module when the returned code runs (which is the module if you callmacroexpand
) The difference is demonstrated belowThis makes no difference when used in the REPL and as I said the current
@macroexpand
might be slightly better but this should still be documented.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.
Can you give a usecase this would actually make a difference? And why is
@macroexpand
potentially better for interactive use?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.
That's exactly what the code above is showing.
I said