Skip to content

Commit

Permalink
fixed issue with inheriting specialized parametrics
Browse files Browse the repository at this point in the history
  • Loading branch information
WschW committed Oct 16, 2018
1 parent 775aa2e commit c59880d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors = ["Warren Schweigert"]
name = "StructuralInheritance"
uuid = "8444d97c-b5e1-11e8-1bb1-4d91caf0c934"
version = "0.1.1"
version = "0.2.0"
license = "MIT"


Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,3 @@ S(2, 4, 8)
```
It is worth noting that this cannot be used with new() as new does not permit
splatting.

## Note: inheriting from a specialized parametric struct is not currently supported
32 changes: 20 additions & 12 deletions src/StructuralInheritance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ isfunction(x::Expr) = x.head == :call
ispath(x) = false
ispath(x::Expr) = x.head == :.

iscontainerlike(x) = false
iscontainerlike(x::Expr) = (x.head in [:vect,:hcat,:row,
:vcat, :call,
:tuple,:curly])

function getpath(x)
oldpath = Symbol[]
while ispath(x)
Expand Down Expand Up @@ -193,23 +198,17 @@ end
"""
adds source module information to the type name
"""
function fulltypename(x,__module__,inhibit = [])
fulltypename(x,__module__,inhibit = []) = x #is a literal

function fulltypename(x::Union{Expr,Symbol},__module__,inhibit = [])
if x in inhibit
return x
end
if isparametric(x) || isfunction(x)
if iscontainerlike(x)
fullargs = [fulltypename(y,__module__,inhibit) for y in x.args]
return Expr(x.head,fullargs...)
end

if typeof(x) <: Expr && (x.head in [:vect,:hcat,:row,:vcat])
return Expr(x.head,[fulltypename(y,__module__,inhibit) for y in x.args]...)
end

if !(typeof(x) <: Expr) && !(typeof(x) <: Symbol)
return x #must be a primitive
end

oldpath,x = getpath(x)

modulePath = append!(Any[fullname(__module__)...],oldpath)
Expand Down Expand Up @@ -244,9 +243,18 @@ update parameters from old fields
"""
function updateParameters(oldFields,oldParams,parameters,parentType,__module__)
newFields = deepcopy(oldFields)
update(x::Symbol) = x
function update(x)
update(x) = x
function update(x::Expr)
y = deepcopy(x)
if isfunction(x) || isparametric(x)
fullargs = [update(z) for z in y.args]
y = Expr(y.head,fullargs...)
end

if iscontainerlike(x)
y = Expr(y.head,[update(z) for z in y.args]...)
end

if y.args[2] in oldParams
loc = findfirst(y->(y==x.args[2]),oldParams)
newParam = parameters[2][loc]
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ end) == ProtoDD_2
f_e::Complex
end) == ProtoO

@test_broken @protostruct( mutable struct O_2 <: DD_2{Int,Real}
@test @protostruct( mutable struct O_2 <: DD_2{Int,Real}
f_e::Complex
end) == ProtoO
end) == ProtoO_2

@test fieldnames(O) == (:f_a,:f_b,:f_c,:f_d,:f_e)
@test fieldtype.(O,[1,2,3,4,5]) == [Int,Real,Complex,Int,Complex]

#@test fieldnames(O) == (:f_a,:f_b,:f_c,:f_d,:f_e)
#@test fieldtype.(O,[1,2,3,4,5]) == [Int,Real,Complex{Int},Int,Complex]
@test fieldnames(O_2) == (:f_a,:f_b,:f_c,:f_d,:f_e)
@test fieldtype.(O_2,[1,2,3,4,5]) == [Int,Real,Complex{Int},Int,Complex]

module M_literal_func
using Main.StructuralInheritance
Expand Down

0 comments on commit c59880d

Please sign in to comment.