Skip to content

Commit

Permalink
got struct defaults working
Browse files Browse the repository at this point in the history
  • Loading branch information
cscherrer committed Jul 9, 2021
1 parent afeacaf commit 4cbc5e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/KeywordCalls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ function _kwcall(__module__, ex)
return $f_esc(NamedTuple{($(argnames...),)}(nt))
end
end


if !static_hasmethod(has_kwargs, Tuple{typeof(f)})
inst = instance_type(f)

if !static_hasmethod(has_kwargs, Tuple{inst})
namedtuplemethod = quote
@inline function $f_esc(nt::NamedTuple)
aliased = $alias($f, nt)
Expand All @@ -78,7 +79,7 @@ function _kwcall(__module__, ex)

kwmethod = quote
$f_esc(;kw...) = $f_esc(NamedTuple(kw))
KeywordCalls.has_kwargs(::typeof($f_esc)) = true
KeywordCalls.has_kwargs(::inst) = true
end

push!(q.args, kwmethod)
Expand Down Expand Up @@ -184,7 +185,8 @@ function _kwalias(f, aliasmap)
end

# See https://github.com/cscherrer/KeywordCalls.jl/issues/22
instance_type(f::F) where {F<:Function} = F
instance_type(f::UnionAll) = Type{f}
@inline instance_type(f::F) where {F<:Function} = F
@inline instance_type(f::UnionAll) = Type{f}


end # module
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ end
end

@testset "Constructors with defaults" begin
@test_broken @inferred Bar((b=1,a=2)).nt == (a=2,b=1,c=0)
@test_broken @inferred Bar((b=1,a=2,c=5)).nt == (a=2,b=1,c=5)
@test @inferred Bar((b=1,a=2)).nt == (a=2,b=1,c=0)
@test @inferred Bar((b=1,a=2,c=5)).nt == (a=2,b=1,c=5)
end

@testset "Keyword aliases" begin
Expand Down

0 comments on commit 4cbc5e2

Please sign in to comment.