Skip to content

Commit

Permalink
copy-free delegating of rest keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed May 16, 2017
1 parent 4f41043 commit b4f5ef9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,9 @@ function vector_any(xs::ANY...)
a
end

function as_kwargs(xs::Union{AbstractArray,Associative})
as_kwargs(xs::Union{AbstractArray,Associative}) = collect_as_kwargs(xs)

function collect_as_kwargs(xs::Union{AbstractArray,Associative})
n = length(xs)
to = Vector{Any}(n*2)
i = 1
Expand Down Expand Up @@ -382,6 +384,8 @@ end
const KWDict = ImmutableDict{Symbol,Any}
const EmptyKWDict = KWDict()

as_kwargs(xs::KWDict) = xs

"""
ImmutableDict
Expand Down
10 changes: 9 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@
`((|::|
;; if there are optional positional args, we need to be able to reference the function name
,(if (any kwarg? pargl) (gensy) UNUSED)
(call (core kwftype) ,ftype)) (:: ,kw (core AnyVector)) ,@pargl ,@vararg)
(call (core kwftype) ,ftype)) ,kw ,@pargl ,@vararg)
`(block
;; initialize keyword args to their defaults, or set a flag telling
;; whether this keyword needs to be set.
Expand All @@ -486,6 +486,13 @@
flags)
,@(if (null? restkw) '()
`((= ,rkw (top EmptyKWDict))))
,(if (and (not (null? restkw)) (null? keynames))
`(if (call (core isa) ,kw (top KWDict))
(block
(= ,rkw ,kw) ;; reuse kwarg list for delegation if possible
(symbolicgoto do_call)))
`(if (call (core isa) ,kw (top KWDict))
(= ,kw (call (top collect_as_kwargs) ,kw))))
;; for i = 1:(length(kw)>>1)
(for (= ,i (: (call (top >>) (call (top length) ,kw) 1) -1 1))
(block
Expand Down Expand Up @@ -546,6 +553,7 @@
'()
`((if ,flag (= ,name ,dflt)))))
keynames vals flags))
(symboliclabel do_call)
;; finally, call the core function
(return (call ,mangled
,@keynames
Expand Down

0 comments on commit b4f5ef9

Please sign in to comment.