Skip to content
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

Merged
merged 8 commits into from
Feb 3, 2017
Merged

doctests for metaprogramming #20302

merged 8 commits into from
Feb 3, 2017

Conversation

KristofferC
Copy link
Member

No description provided.

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)))))))))
Copy link
Member Author

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

@KristofferC KristofferC mentioned this pull request Jan 28, 2017
38 tasks
@KristofferC KristofferC assigned kshyatt and unassigned kshyatt Jan 28, 2017
@@ -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;
Copy link
Member

Choose a reason for hiding this comment

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

? 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Derp

@kshyatt kshyatt added the docs This change adds or pertains to documentation label Jan 28, 2017
@kshyatt
Copy link
Contributor

kshyatt commented Jan 28, 2017

Why am I assigned but not a reviewer? What are you trying to tell me?? 😆

@KristofferC
Copy link
Member Author

I thought that was the same thing sortof...

@@ -263,29 +263,28 @@ julia> eval(ans)
julia> ex = :(a + b)
:(a + b)

julia> eval(ex)
julia> eval(current_module(), ex)
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

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

@@ -393,23 +393,24 @@ instances of `@sayhello` with:
When `@sayhello` is given at the REPL, the expression executes immediately, thus we only see the
Copy link
Contributor

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


julia> # start some other operations that use f(x):
Start some other operations that use f(x):
Copy link
Contributor

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)"


julia> # now we add some new definitions for f(x):
We now add some new definitions for f(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

Forgot backticks

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:
Copy link
Contributor

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

return :($ex ? nothing : throw(AssertionError($msg)))
end
@assert (macro with 1 method)

Copy link
Contributor

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


julia> # start some other operations that use f(x):
Start some other operations that use f(x):
Copy link
Contributor

Choose a reason for hiding this comment

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

code highlight f(x)

@@ -419,9 +420,9 @@ this is an extremely useful tool for debugging macros):

```julia
julia> ex = macroexpand( :(@sayhello("human")) )
Copy link
Member

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?

Copy link
Member

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


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:
Copy link
Contributor

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 [ ]

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, will update.


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))))
Copy link
Member Author

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"

@KristofferC
Copy link
Member Author

Updated for newest documenter (could remove current_module()). All doctest + whitespace passes locally.

end
return :($ex + 1)
end
sub2ind_gen (generic function with 1 method)
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, makes sense

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@KristofferC
Copy link
Member Author

KristofferC commented Jan 31, 2017

Updated to test another function. Doctests still passes locally.

@KristofferC KristofferC merged commit 2f8ad04 into master Feb 3, 2017
@KristofferC KristofferC deleted the kc/doctest_metaprogramming branch February 3, 2017 17:34
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
Copy link
Contributor

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)

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