|
46 | 46 |
|
47 | 47 | foreachfield(f, x::T, xs...) where {T} = foreachfield(staticschema(T), f, x, xs...) |
48 | 48 |
|
49 | | -createinstance(::Type{T}, args...) where {T} = T(args...) |
50 | | -createinstance(::Type{T}, args...) where {T<:Union{Tuple, NamedTuple}} = T(args) |
51 | | - |
52 | | -add_params(::Type{T}, ::Type{C}) where {T, C<:Tuple} = T |
53 | | -add_params(::Type{T}, ::Type{C}) where {T<:Tuple, C<:Tuple} = C |
54 | | -add_params(::Type{<:NamedTuple{names}}, ::Type{C}) where {names, C<:Tuple} = NamedTuple{names, C} |
55 | | -add_params(::Type{<:Pair}, ::Type{Tuple{S, T}}) where {S, T} = Pair{S, T} |
56 | | - |
57 | 49 | """ |
58 | 50 | `iscompatible(::Type{S}, ::Type{V}) where {S, V<:AbstractArray}` |
59 | 51 |
|
60 | 52 | Check whether element type `S` can be pushed to a container of type `V`. |
61 | 53 | """ |
62 | 54 | iscompatible(::Type{S}, ::Type{<:AbstractArray{T}}) where {S, T} = S<:T |
63 | | -iscompatible(::Type{S}, ::Type{StructArray{T, N, C}}) where {S, T, N, C} = iscompatible(astuple(staticschema(S)), astuple(C)) |
| 55 | +iscompatible(::Type{S}, ::Type{<:StructArray{<:Any, <:Any, C}}) where {S, C} = iscompatible(astuple(staticschema(S)), astuple(C)) |
64 | 56 |
|
65 | 57 | iscompatible(::Type{Tuple{}}, ::Type{T}) where {T<:Tuple} = false |
66 | 58 | iscompatible(::Type{T}, ::Type{Tuple{}}) where {T<:Tuple} = false |
|
72 | 64 |
|
73 | 65 | iscompatible(::S, ::T) where {S, T<:AbstractArray} = iscompatible(S, T) |
74 | 66 |
|
| 67 | +function _promote_typejoin(::Type{S}, ::Type{T}) where {S<:NTuple{N, Any}, T<:NTuple{N, Any}} where N |
| 68 | + head = _promote_typejoin(Base.tuple_type_head(S), Base.tuple_type_head(T)) |
| 69 | + tail = _promote_typejoin(Base.tuple_type_tail(S), Base.tuple_type_tail(T)) |
| 70 | + return Base.tuple_type_cons(head, tail) |
| 71 | +end |
| 72 | + |
| 73 | +_promote_typejoin(::Type{Tuple{}}, ::Type{Tuple{}}) = Tuple{} |
| 74 | +function _promote_typejoin(::Type{NamedTuple{names, types}}, ::Type{NamedTuple{names, types′}}) where {names, types, types′} |
| 75 | + T = _promote_typejoin(types, types′) |
| 76 | + return NamedTuple{names, T} |
| 77 | +end |
| 78 | + |
| 79 | +_promote_typejoin(::Type{S}, ::Type{T}) where {S, T} = Base.promote_typejoin(S, T) |
| 80 | + |
| 81 | +function _promote_typejoin(::Type{Pair{A, B}}, ::Type{Pair{A′, B′}}) where {A, A′, B, B′} |
| 82 | + C = _promote_typejoin(A, A′) |
| 83 | + D = _promote_typejoin(B, B′) |
| 84 | + return Pair{C, D} |
| 85 | +end |
| 86 | + |
75 | 87 | function replace_storage(f, v::AbstractArray{T, N})::AbstractArray{T, N} where {T, N} |
76 | 88 | f(v) |
77 | 89 | end |
@@ -106,9 +118,15 @@ function replace_storage(f, s::StructArray{T}) where T |
106 | 118 | StructArray{T}(newcols) |
107 | 119 | end |
108 | 120 |
|
109 | | -to_tup(c::T) where {T} = to_tup(c, fields(T)) |
| 121 | +to_tup(c::T) where {T} = to_tup(c, fieldnames(staticschema(T))) |
110 | 122 | function to_tup(c, fields::NTuple{N, Symbol}) where N |
111 | 123 | t = ntuple(i -> getproperty(c, fields[i]), N) |
112 | 124 | return NamedTuple{fields}(t) |
113 | 125 | end |
114 | 126 | to_tup(c, fields::NTuple{N, Int}) where {N} = ntuple(i -> _getproperty(c, fields[i]), N) |
| 127 | + |
| 128 | +astuple(::Type{NamedTuple{names, types}}) where {names, types} = types |
| 129 | +astuple(::Type{T}) where {T<:Tuple} = T |
| 130 | + |
| 131 | +strip_params(::Type{<:Tuple}) = Tuple |
| 132 | +strip_params(::Type{<:NamedTuple{names}}) where {names} = NamedTuple{names} |
0 commit comments