Skip to content

Commit 4b175b0

Browse files
authored
Merge pull request #11 from invenia/ox/sigwhere
fix whereparams that are unions
2 parents 5b643de + 1461ecd commit 4b175b0

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ExprTools"
22
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
33
authors = ["Invenia Technical Computing"]
4-
version = "0.1.1"
4+
version = "0.1.2"
55

66
[compat]
77
julia = "1"

src/method.jl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function signature(m::Method)
3434

3535
def[:args] = arguments(m)
3636
def[:whereparams] = where_parameters(m)
37-
def[:params] = parameters(m)
37+
def[:params] = type_parameters(m)
3838
def[:kwargs] = kwargs(m)
3939

4040
return Dict(k => v for (k, v) in def if v !== nothing) # filter out nonfields.
@@ -52,10 +52,10 @@ function argument_names(m::Method)
5252
return arg_names
5353
end
5454

55-
56-
function argument_types(m::Method)
55+
argument_types(m::Method) = argument_types(m.sig)
56+
function argument_types(sig)
5757
# First parameter of `sig` is the type of the function itself
58-
return parameters(m.sig)[2:end]
58+
return parameters(sig)[2:end]
5959
end
6060

6161
name_of_type(x) = x
@@ -74,7 +74,10 @@ function name_of_type(x::UnionAll)
7474
whereparam = where_parameters(x.var)
7575
return :($name where $whereparam)
7676
end
77-
77+
function name_of_type(x::Union)
78+
parameter_names = name_of_type.(Base.uniontypes(x))
79+
return :(Union{$(parameter_names...)})
80+
end
7881

7982
function arguments(m::Method)
8083
arg_names = argument_names(m)
@@ -96,28 +99,28 @@ function where_parameters(x::TypeVar)
9699
if x.lb === Union{} && x.ub === Any
97100
return x.name
98101
elseif x.lb === Union{}
99-
return :($(x.name) <: $(Symbol(x.ub)))
102+
return :($(x.name) <: $(name_of_type(x.ub)))
100103
elseif x.ub === Any
101-
return :($(x.name) >: $(Symbol(x.lb)))
104+
return :($(x.name) >: $(name_of_type(x.lb)))
102105
else
103-
return :($(Symbol(x.lb)) <: $(x.name) <: $(Symbol(x.ub)))
106+
return :($(name_of_type(x.lb)) <: $(x.name) <: $(name_of_type(x.ub)))
104107
end
105108
end
106109

107-
function where_parameters(m::Method)
108-
m.sig isa UnionAll || return nothing
109-
110+
where_parameters(m::Method) = where_parameters(m.sig)
111+
where_parameters(sig) = nothing
112+
function where_parameters(sig::UnionAll)
110113
whereparams = []
111-
sig = m.sig
112114
while sig isa UnionAll
113115
push!(whereparams, where_parameters(sig.var))
114116
sig = sig.body
115117
end
116118
return whereparams
117119
end
118120

119-
function parameters(m::Method)
120-
typeof_type = first(parameters(m.sig)) # will be e.g Type{Foo{P}} if it has any parameters
121+
type_parameters(m::Method) = type_parameters(m.sig)
122+
function type_parameters(sig)
123+
typeof_type = first(parameters(sig)) # will be e.g Type{Foo{P}} if it has any parameters
121124
typeof_type <: Type{<:Any} || return nothing
122125

123126
function_type = first(parameters(typeof_type)) # will be e.g. Foo{P}

test/method.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ end
7272

7373
@test_signature f12(x::S) where {S>:Integer} = 2x
7474
@test_signature f13(x::S) where Integer<:S<:Number = 2x
75+
76+
@test_signature f_where_union(x::T) where T<:Union{Bool, Int32} = 2x
7577
end
7678

7779
@testset "Arg types with type-parameters" begin

0 commit comments

Comments
 (0)