Skip to content

Commit

Permalink
Simplify longdef1 and handle f(...) where T where U where ... = ...
Browse files Browse the repository at this point in the history
  • Loading branch information
cstjean committed Aug 11, 2017
1 parent 51391fb commit 9d701da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/utils.jl
Expand Up @@ -154,19 +154,20 @@ isdef(ex) = ismatch(or_(:(function _(__) _ end),
:(f_(__) = _)),
ex)

isshortdef(ex) = (@capture(ex, (fcall_ = body_)) &&
(@capture(fcall, (f_(args__) |
f_(args__)::rtype_)) ||
# This catches f(...) where T where U
(@capture(fcall, fcall2_ where _) && isshortdef(:($fcall2 = $body)))))

function longdef1(ex)
@match ex begin
(f_(args__) = body_) => @q function $f($(args...)) $body end
(f_(args__)::rtype_ = body_) => @q function $f($(args...))::$rtype $body end
((args__,) -> body_) => @q function ($(args...),) $body end
(arg_ -> body_) => @q function ($arg,) $body end
(f_(args__) where {whereparams__} = body_) =>
@q function $f($(args...)) where {$(whereparams...)}
$body end
((f_(args__)::rtype_) where {whereparams__} = body_) =>
@q function ($f($(args...))::$rtype) where {$(whereparams...)}
$body end
_ => ex
if @capture(ex, (arg_ -> body_))
@q function ($arg,) $body end
elseif isshortdef(ex)
@assert @capture(ex, (fcall_ = body_))
striplines(Expr(:function, fcall, body))
else
ex
end
end
longdef(ex) = prewalk(longdef1, ex)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Expand Up @@ -95,6 +95,7 @@ let
# Ideally we'd compare the result against :(function f(x)::Int 10 end),
# but it fails because of :line and :block differences
@test longdef(:(f(x)::Int = 10)).head == :function
@test longdef(:(f(x::T) where U where T = 2)).head == :function
@test shortdef(:(function f(x)::Int 10 end)).head != :function
@test map(splitarg, (:(f(a=2, x::Int=nothing, y, args...))).args[2:end]) ==
[(:a, :Any, false, 2), (:x, :Int, false, :nothing),
Expand Down

0 comments on commit 9d701da

Please sign in to comment.