-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Description
Currently, we can perform a limited set of logic in the type definition, such as:
type A
a::eltype(Vector{Int})
endbut we can't involve a type parameter, such as:
type B{V <: AbstractVector}
a::eltype(V)
endAFAICT, at the moment the field types A.types is calculated when the type is defined and type parameters are inserted into the correct slots as they become known.
However, it would be nice if the types could be calculated by arbitrary inferrable or @pure functions. Another simple example (close to my heart) would be:
immutable StaticMatrix{M,N,T}
data::NTuple{M*N, T}
endHowever this results in an error that multiplication is not defined for TypeVars. Instead, I need all of this code:
immutable StaticMatrix{M,N,T,L}
data::NTuple{L, T}
function StaticMatrix(d)
check_params(Val{L}, Val{M}, Val{N})
new(d)
end
end
@generated function check_params{L,M,N}(::Type{Val{L}}, ::Type{Val{M}}, ::Type{Val{N}}) # could also be `@pure` in v0.5
if L != M*N
error("Type parameters don't match")
end
endand my users need to foist around the redundant L paramater when they need to specify a concrete type.
For abstract types, I'm hoping that inference itself could still be used to come up with a least-pessimistic approximation of each field, or otherwise just use Any when that's not possible. If that makes it difficult to avoid regressions, straight types (combinations of types and the relevant TypeVars with apply_type but no other functions) could keep functioning as they currently do.