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

make macros generic functions #14563

Merged
merged 1 commit into from
Jan 5, 2016
Merged

make macros generic functions #14563

merged 1 commit into from
Jan 5, 2016

Conversation

JeffBezanson
Copy link
Sponsor Member

Here's another tidbit from jb/functions that's easy to apply to master.

fixes #9627, #8846, and #8701, and might help #3377

fix #9627, fix #8846, fix #8701
helps #3377, but additional work is needed there
@quinnj
Copy link
Member

quinnj commented Jan 5, 2016

Seems like a first-class kind of solution 😎

@StefanKarpinski
Copy link
Sponsor Member

Love it. Since all the tests pass, should we merge this?

JeffBezanson added a commit that referenced this pull request Jan 5, 2016
@JeffBezanson JeffBezanson merged commit 84a4b3f into master Jan 5, 2016
@mlubin
Copy link
Member

mlubin commented Jan 5, 2016

Nice!

@tkelman
Copy link
Contributor

tkelman commented Jan 5, 2016

If it fixes that many issues, could use a few more test cases couldn't it?

@tkelman tkelman deleted the jb/genericmacros branch January 5, 2016 16:11
@mbauman
Copy link
Sponsor Member

mbauman commented Jan 5, 2016

Probably could use a NEWS entry, too. This is very nifty:

julia> macro foo(x::Int)
           :($x + 1)
       end
@foo (macro with 1 method)

julia> macro foo(x::Symbol)
         @show x
       end
@foo (macro with 2 methods)

julia> @foo 1
2

julia> @foo show
x = :show
show (generic function with 122 methods)

julia> @foo 1+2
ERROR: MethodError: `@foo` has no method matching @foo(::Expr)
 in eval at ./boot.jl:265

@timholy
Copy link
Sponsor Member

timholy commented Jan 5, 2016

Pretty awe-inspiring that this comes with a net reduction of 13 lines of code, and a churn < 100 lines.

@tkelman
Copy link
Contributor

tkelman commented Jan 5, 2016

Merging this appears to have introduced a failure on 32 bit appveyor: https://ci.appveyor.com/project/StefanKarpinski/julia/build/1.0.12336/job/smqeb0ravuyaroa3

@yuyichao
Copy link
Contributor

yuyichao commented Jan 5, 2016

Hmm, I've seen this failure appears on #14541 lin32 for some time and haven't been able to figure out why. I suspect it's related to the IOContext/ImmutableDict and very likely also related to the UndefRefError in dict test and a similar one I've just reproduced repeatedly on ARM.

@Ismael-VC
Copy link
Contributor

I understand that this would replace generated functions, am I right?

On generated functions:

These have the capability to generate specialized code depending on the types of their arguments with more flexibility and/or less code than what can be achieved with multiple dispatch.

Edit: changed generic fucntions to generated functions, sorry ...muscle memory.

@JeffBezanson
Copy link
Sponsor Member Author

No, after this macros are basically the same as they always were. But the implementation is changed so that a bit more flexibility exists, e.g. having separate definitions for cases with different numbers of arguments.

@iamed2
Copy link
Contributor

iamed2 commented Jan 6, 2016

@JeffBezanson but @mbauman's example would suggest that they are not the same as they always were? In that you can now have different implementations for different input types.

@JeffBezanson
Copy link
Sponsor Member Author

Yes, but for the types of the expression objects, not the types of run time values. So the types are almost always just Expr.

@iamed2
Copy link
Contributor

iamed2 commented Jan 6, 2016

Ah, understood. Thanks.

@Ismael-VC
Copy link
Contributor

Yes, but for the types of the expression objects, not the types of run time values. So the types are almost always just Expr.

Oh! that makes a lot of sense now, thank you very much!

@MichaelHatherly
Copy link
Member

Super cool! Unless I've missed something we can't extend a macro without first explicitly importing it, ie.

module A

macro m(x::Int)
    "an integer"
end

end

# I'd expect this to work as it does for methods
macro A.m(x::Expr)
    "an expression"
end

# but it throws a
#
# ERROR: syntax: invalid macro definition
#  in eval at ./boot.jl:265

# this does work as expected though
import .A: @m
macro m(x::Expr)
    "an expression"
end
julia> @m 1
"an integer"

julia> @m 1 + 2
"an expression"

Would it be too greedy to ask for macro A.m syntax as well? :)

MichaelHatherly added a commit to MichaelHatherly/julia that referenced this pull request Jan 6, 2016
Since JuliaLang#14563 made macros into generic functions we should naturally
handle them in the same way as functions in the docsystem.

Since the expression `@time` is no different to `@time()` this is
slightly problematic for querying the docsystem for the docs associated
with the `Function` rather than any one particular `Method` of the
macro. Hence I've added the syntax `help?> :@time` to get docs for the
whole macro while `help?> @time`, `help?> @time(x)`, etc. will get docs
for a particular signature. The quote (`:`) syntax mirrors that of

    "..."
    :@time

syntax which is already available for use to document `@time` so using
it for retrieving as well as setting docs doesn't seem too much of a
stretch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default values for macros