Skip to content

Commit

Permalink
Merge pull request #16627 from JuliaLang/teh/dispatch_varargN
Browse files Browse the repository at this point in the history
Check for Vararg{T,N} in typemap: sig_match_by_type_simple
  • Loading branch information
timholy committed Jun 1, 2016
2 parents a3f2285 + ea18b98 commit 002f081
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/typemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ static int sig_match_by_type_simple(jl_value_t **types, size_t n, jl_tupletype_t
}
if (va) {
jl_value_t *decl = jl_field_type(sig, i);
if (jl_vararg_kind(decl) == JL_VARARG_INT) {
if (n-i != jl_unbox_long(jl_tparam1(decl)))
return 0;
}
jl_value_t *t = jl_tparam0(decl);
for(; i < n; i++) {
if (!jl_subtype(types[i], t, 0))
Expand Down Expand Up @@ -150,6 +154,10 @@ static inline int sig_match_simple(jl_value_t **args, size_t n, jl_value_t **sig
}
if (va) {
jl_value_t *decl = sig[i];
if (jl_vararg_kind(decl) == JL_VARARG_INT) {
if (n-i != jl_unbox_long(jl_tparam1(decl)))
return 0;
}
jl_value_t *t = jl_tparam0(decl);
for(; i < n; i++) {
if (!jl_subtype(args[i], t, 1))
Expand Down
22 changes: 17 additions & 5 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,23 @@ Type11167{Float32,5}

# dispatch
let
local foo, bar, baz
foo(x::Tuple{Vararg{Any}})=0
foo(x::Tuple{Vararg{Integer}})=1
@test foo((:a,))==0
@test foo(( 2,))==1
local foo, foo2, fooN, bar, baz
foo(x::Tuple{Vararg{Any}}) = 0
foo(x::Tuple{Vararg{Integer}}) = 1
@test foo((:a,)) == 0
@test foo(( 2,)) == 1

foo2(x::Vararg{Any,2}) = 2
@test foo2(1,2) == 2
@test_throws MethodError foo2(1)
@test_throws MethodError foo2(1,2,3)

fooN{T,N}(A::Array{T,N}, x::Vararg{Any,N}) = -1
@test fooN([1,2], 1) == -1
@test_throws MethodError fooN([1,2], 1, 2) == -1
@test fooN([1 2; 3 4], 1, 2) == -1
@test_throws MethodError fooN([1 2; 3 4], 1)
@test_throws MethodError fooN([1 2; 3 4], 1, 2, 3)

bar{T}(x::Tuple{T,T,T,T})=1
bar(x::Tuple{Any,Any,Any,Any})=2
Expand Down

0 comments on commit 002f081

Please sign in to comment.