Skip to content

Commit 7101f29

Browse files
authored
Merge pull request #100 from marius311/combinedef_improv
more faithfully reconstruct original function in combinedef
2 parents c8a0bf1 + 7168562 commit 7101f29

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/utils.jl

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ isshortdef(ex) = (@capture(ex, (fcall_ = body_)) &&
206206

207207
function longdef1(ex)
208208
if @capture(ex, (arg_ -> body_))
209-
@q function ($arg,) $body end
209+
@q function ($arg,) $(body.args...) end
210210
elseif isshortdef(ex)
211211
@assert @capture(ex, (fcall_ = body_))
212212
striplines(Expr(:function, fcall, body))
@@ -218,12 +218,13 @@ longdef(ex) = prewalk(longdef1, ex)
218218

219219
function shortdef1(ex)
220220
@match ex begin
221-
function f_(args__) body_ end => @q $f($(args...)) = $body
222-
function f_(args__) where T__ body_ end => @q $f($(args...)) where $(T...) = $body
223-
function f_(args__)::rtype_ body_ end => @q $f($(args...))::$rtype = $body
224-
function (args__,) body_ end => @q ($(args...),) -> $body
221+
function f_(args__) body_ end => @q $f($(args...)) = $(body.args...)
222+
function f_(args__) where T__ body_ end => @q $f($(args...)) where $(T...) = $(body.args...)
223+
function f_(args__)::rtype_ body_ end => @q $f($(args...))::$rtype = $(body.args...)
224+
function f_(args__)::rtype_ where T__ body_ end => @q ($f($(args...))::$rtype) where $(T...) = $(body.args...)
225+
function (args__,) body_ end => @q ($(args...),) -> $(body.args...)
225226
((args__,) -> body_) => ex
226-
(arg_ -> body_) => @q ($arg,) -> $body
227+
(arg_ -> body_) => @q ($arg,) -> $(body.args...)
227228
_ => ex
228229
end
229230
end
@@ -289,23 +290,38 @@ end
289290
`combinedef` is the inverse of `splitdef`. It takes a splitdef-like Dict
290291
and returns a function definition. """
291292
function combinedef(dict::Dict)
292-
rtype = get(dict, :rtype, :Any)
293+
rtype = get(dict, :rtype, nothing)
293294
params = get(dict, :params, [])
294295
wparams = get(dict, :whereparams, [])
296+
body = block(dict[:body])
295297
name = dict[:name]
296298
name_param = isempty(params) ? name : :($name{$(params...)})
297299
# We need the `if` to handle parametric inner/outer constructors like
298300
# SomeType{X}(x::X) where X = SomeType{X}(x, x+2)
299301
if isempty(wparams)
300-
:(function $name_param($(dict[:args]...);
301-
$(dict[:kwargs]...))::$rtype
302-
$(dict[:body])
303-
end)
302+
if rtype==nothing
303+
@q(function $name_param($(dict[:args]...);
304+
$(dict[:kwargs]...))
305+
$(body.args...)
306+
end)
307+
else
308+
@q(function $name_param($(dict[:args]...);
309+
$(dict[:kwargs]...))::$rtype
310+
$(body.args...)
311+
end)
312+
end
304313
else
305-
:(function $name_param($(dict[:args]...);
306-
$(dict[:kwargs]...))::$rtype where {$(wparams...)}
307-
$(dict[:body])
308-
end)
314+
if rtype==nothing
315+
@q(function $name_param($(dict[:args]...);
316+
$(dict[:kwargs]...)) where {$(wparams...)}
317+
$(body.args...)
318+
end)
319+
else
320+
@q(function $name_param($(dict[:args]...);
321+
$(dict[:kwargs]...))::$rtype where {$(wparams...)}
322+
$(body.args...)
323+
end)
324+
end
309325
end
310326
end
311327

0 commit comments

Comments
 (0)