-
-
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
doctests for metaprogramming #20302
doctests for metaprogramming #20302
Conversation
doc/src/manual/metaprogramming.md
Outdated
julia> typeof(ex) | ||
Expr | ||
|
||
julia> ex | ||
:(println("I execute at runtime. The argument is: ",$(Expr(:copyast, :(:((1,2,3))))))) | ||
:((println)("I execute at runtime. The argument is: ",$(Expr(:copyast, :($(QuoteNode(:((1,2,3))))))))) |
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.
This printing seems to be a bit less nice now perhaps
doc/src/manual/metaprogramming.md
Outdated
@@ -305,9 +304,9 @@ julia> a = 1; | |||
julia> ex = Expr(:call, :+, a, :b) | |||
:(1 + b) | |||
|
|||
julia> a = 0; b = 2; | |||
julia> a = 0; b = 2; |
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.
? 😄
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.
Derp
Why am I assigned but not a reviewer? What are you trying to tell me?? 😆 |
I thought that was the same thing sortof... |
doc/src/manual/metaprogramming.md
Outdated
@@ -263,29 +263,28 @@ julia> eval(ans) | |||
julia> ex = :(a + b) | |||
:(a + b) | |||
|
|||
julia> eval(ex) | |||
julia> eval(current_module(), ex) |
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.
should we add some text explaining what current_module()
is and why it's 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.
Yeah probably. It's not neeed in the REPL but since the doctests are run in a separate module I think it is needed.
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'd say that's a bug in Documenter, shouldn't need to jump through those kind of hoops. I'll see what can be done about it.
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!
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.
If that gets resolved then the closest-candidates from #20224 (comment) can probably be brought back too
doc/src/manual/metaprogramming.md
Outdated
@@ -393,23 +393,24 @@ instances of `@sayhello` with: | |||
When `@sayhello` is given at the REPL, the expression executes immediately, thus we only see the |
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.
"given at the REPL" is weird phrasing
doc/src/manual/metaprogramming.md
Outdated
|
||
julia> # start some other operations that use f(x): | ||
Start some other operations that use f(x): |
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.
As a standalone sentence this doesn't work very well. I'd say something like "define other operations that use f(x)
"
doc/src/manual/metaprogramming.md
Outdated
|
||
julia> # now we add some new definitions for f(x): | ||
We now add some new definitions for f(x): |
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.
Forgot backticks
doc/src/manual/metaprogramming.md
Outdated
end | ||
return :($ex + 1) | ||
end | ||
sub2ind_gen (generic function with 1 method) | ||
``` | ||
|
||
**What code will this generate?** | ||
|
||
An easy way to find out, is to extract the body into another (regular) function: |
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.
again not your fault but this comma shouldn't be here
doc/src/manual/metaprogramming.md
Outdated
return :($ex ? nothing : throw(AssertionError($msg))) | ||
end | ||
@assert (macro with 1 method) | ||
|
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.
no need for the empty line
doc/src/manual/metaprogramming.md
Outdated
|
||
julia> # start some other operations that use f(x): | ||
Start some other operations that use f(x): |
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.
code highlight f(x)
6da1d5c
to
17c14f6
Compare
doc/src/manual/metaprogramming.md
Outdated
@@ -419,9 +420,9 @@ this is an extremely useful tool for debugging macros): | |||
|
|||
```julia | |||
julia> ex = macroexpand( :(@sayhello("human")) ) |
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.
Perhaps clearer by using the @macroexpand
macro?
julia> ex = @macroexpand @sayhello "human"
:((Main.println)("Hello, ","human"))
to avoid the extra quoting stuff?
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.
And could be doctested if the note on the interpolated string is extracted and added afterwards instead
doc/src/manual/metaprogramming.md
Outdated
|
||
julia> typeof(ex) | ||
Expr | ||
``` | ||
|
||
We can see that the `"human"` literal has been interpolated into the expression. | ||
|
||
There also exists a macro [`@macroexpand`][@ref] that is perhaps a bit more convenient than the `macroexpand` function: |
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.
@ref
should be in ( ) not [ ]
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.
Sorry, will update.
doc/src/manual/metaprogramming.md
Outdated
|
||
julia> ex = macroexpand( :(@twostep :(1, 2, 3)) ); | ||
I execute at parse time. The argument is: :((1,2,3)) | ||
I execute at parse time. The argument is: $(Expr(:quote, :((1,2,3)))) |
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.
This printing is also a bit less "nice"
88e3772
to
13c0a8b
Compare
Updated for newest documenter (could remove |
end | ||
return :($ex + 1) | ||
end | ||
sub2ind_gen (generic function with 1 method) |
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.
should this be called to verify that it works?
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.
yeah, makes sense
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.
done
Updated to test another function. Doctests still passes locally. |
2c9b715
to
e5d2d58
Compare
julia> @generated function sub2ind_gen{N}(dims::NTuple{N}, I::Integer...) | ||
return sub2ind_gen_impl(dims, I...) | ||
end | ||
sub2ind_gen (generic function with 1 method) | ||
|
||
julia> function sub2ind_gen_impl{N}(dims::Type{NTuple{N}}, I...) | ||
julia> function sub2ind_gen_impl(dims::Type{T}, I...) where T <: NTuple{N,Any} where N |
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.
might be a little more readable as where T <: (NTuple{N,Any} where N)
No description provided.