Skip to content

Commit

Permalink
Merge pull request #188 from JuliaLang/yyc/call
Browse files Browse the repository at this point in the history
Support kw argument in call overload.
  • Loading branch information
yuyichao committed May 1, 2016
2 parents 5072dab + 353d758 commit 937f7d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,19 +508,29 @@ function _compat(ex::Expr)
# Passthrough
return ex
elseif new_style_call_overload(ex)
if ((ex.args[1]::Expr).args[1]::Expr).head === :(::)
callexpr = ex.args[1]::Expr
callee = callexpr.args[1]::Expr
is_kw = (length(callexpr.args) >= 2 &&
isexpr(callexpr.args[2], :parameters))
if callee.head === :(::)
# (:function, (:call, :(:(::), <1>), <2>), <body>) ->
# (:function, (:call, :(Base.call), :(:(::), <1>), <2>), <body>)
unshift!((ex.args[1]::Expr).args, :(Base.call))
unshift!(callexpr.args, :(Base.call))
else
# (:function, (:call, :(curly, :(:(::), <1>), <3>), <2>), <body>) ->
# (:function, (:call, :(curly, :(Base.call), <3>), :(:(::), <1>), <2>), <body>)
callexpr = ex.args[1]::Expr
callee = callexpr.args[1]::Expr
obj = callee.args[1]::Expr
callee.args[1] = :(Base.call)
insert!(callexpr.args, 2, obj)
end
if is_kw
# Expr(:parameters) is moved to the 3rd argument
params = callexpr.args[3]
@assert isexpr(params, :parameters)
obj = callexpr.args[2]
callexpr.args[2] = params
callexpr.args[3] = obj
end
end
return Expr(ex.head, map(_compat, ex.args)...)
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -879,18 +879,22 @@ if VERSION >= v"0.4"

@compat (a::A)() = a.a
@compat (a::A)(b) = (a.a, b)
@compat (a::A)(b, c; d=2) = (a.a, b, c, d)
@compat (b::B{T}){T}() = b.b, T
@compat (b::B{T}){T}(c::T) = 1
@compat (b::B{T}){T,T2}(c::T2) = 0
@compat (b::B{T}){T}(c::T, d; f=1) = (c, d, f)

@test A() === A(1)
@test B() === B(0)
@test B{Float64}() === B(0.0)

@test A(1)() === 1
@test A(1)(2) === (1, 2)
@test A(1)(2, 3; d=10) === (1, 2, 3, 10)
@test B(0)() === (0, Int)
@test B(0)(1) === 1
@test B(0)(1, 2; f=100) === (1, 2, 100)
@test B(0)(1.0) === 0
end

Expand Down

0 comments on commit 937f7d0

Please sign in to comment.