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 17, 2017
1 parent 01f0b06 commit cf66bb9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 0 additions & 2 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ export
# constants
nothing, Main

const AnyVector = Array{Any,1}

abstract type Number end
abstract type Real <: Number end
abstract type AbstractFloat <: Real end
Expand Down
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
2 changes: 1 addition & 1 deletion base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function arg_decl_parts(m::Method)
end

function kwarg_decl(m::Method, kwtype::DataType)
sig = rewrap_unionall(Tuple{kwtype, Core.AnyVector, unwrap_unionall(m.sig).parameters...}, m.sig)
sig = rewrap_unionall(Tuple{kwtype, Any, unwrap_unionall(m.sig).parameters...}, m.sig)
kwli = ccall(:jl_methtable_lookup, Any, (Any, Any, UInt), kwtype.name.mt, sig, typemax(UInt))
if kwli !== nothing
kwli = kwli::Method
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 cf66bb9

Please sign in to comment.