Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/ConcreteStructs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function _make_constructor(struct_name, type_params, type_params_full, lines)
args = map(x->x.args, field_lines)
vars = first.(args)
var_types = last.(args)
constructor_params = _get_constructor_params(type_params, var_types)
constructor_params = _get_constructor_params(type_params, type_params_full, var_types)
new_params = _strip_super(type_params_full)

if length(type_params) == length(type_params_full) && all(type_params .== type_params_full)
Expand All @@ -156,8 +156,8 @@ end


# Get the parameters that are unmatched to variables and need to be annoted in the constructor
function _get_constructor_params(type_params, var_types)
subparams = _get_subparams(type_params)
function _get_constructor_params(type_params, type_params_full, var_types)
subparams = _get_subparams(type_params_full)
type_params = _strip_super(type_params)
var_types = [subparams; _strip_super(var_types)]
return setdiff(type_params, var_types)
Expand All @@ -167,7 +167,15 @@ end
# Strip supertype annotations
_strip_super(x) = x
_strip_super(x::Union{Tuple, AbstractVector}) = vcat(_strip_super.(x)...)
_strip_super(x::Expr) = x.head == :(<:) ? x.args[1] : x
function _strip_super(x::Expr)
if x.head == :(<:)
return x.args[1]
elseif x.head == :curly
return vcat((_strip_super(y) for y in x.args[2:end])...)
else
return x
end
end


# Get the subparameters of supertypes of subtype parameters (sorry)
Expand Down
28 changes: 24 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ subtype_annotated_assignment = :(a<:Real=2.0)
@test _strip_super(struct_name) == struct_name
@test _strip_super(kitchen_sink) == :(MyStruct{T1, T2<:AbstractVector{T1}})
@test _strip_super([struct_name, sub_typed]) == [struct_name, :(MyStruct{D})]
@test _strip_super(:(Vector{T})) == [:T]
@test _strip_super(:(Vector{Vector{T}})) == [:T]
end

@testset "_get_subparams" begin
Expand All @@ -64,9 +66,12 @@ subtype_annotated_assignment = :(a<:Real=2.0)
end

@testset "_get_constructor_params" begin
@test _get_constructor_params([:T, :(A<:AbstractVector{T})], [:A, :B]) == []
@test _get_constructor_params([:iip, :T, :(A<:AbstractVector{T})], [:A, :B]) == [:iip]
@test _get_constructor_params([:iip, :T, :(A<:AbstractVector{T}), :B], [:B]) == [:iip, :A]
@test _get_constructor_params([:T, :(A<:AbstractVector{T})], [:T, :(A<:AbstractVector{T})], [:A, :B]) == []
@test _get_constructor_params([:iip, :T, :(A<:AbstractVector{T})], [:iip, :T, :(A<:AbstractVector{T})], [:A, :B]) == [:iip]
@test _get_constructor_params([:iip, :T, :(A<:AbstractVector{T}), :B], [:iip, :T, :(A<:AbstractVector{T}), :B], [:B]) == [:iip, :A]
@test _get_constructor_params([:T], [:T], [:(Vector{T}), :A]) == []
@test _get_constructor_params([:T], [:T], [:(Vector{Vector{T}}), :A]) == []
@test _get_constructor_params([:T], [:T, :(A <: Vector{T})], [:A]) == []
end
end

Expand Down Expand Up @@ -234,4 +239,19 @@ end
end
terse_kw_def = TerseKWDef(b = false)
@test typeof(terse_kw_def) === TerseKWDef{Float64, Bool}
end
end

@testset "Issue #18" begin
@concrete struct Foo4{T}
x <: Vector{T}
end
f4 = Foo4(zeros(1))
@test typeof(f4) === Foo4{Float64, Vector{Float64}}

@concrete struct Foo5{T}
x::Vector{T}
y
end
f5 = Foo5(zeros(1), 2)
@test typeof(f5) === Foo5{Float64, Int64}
end