Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructing leaftypes by filling in dependent type parameters #22056

Closed
timholy opened this issue May 25, 2017 · 5 comments
Closed

Constructing leaftypes by filling in dependent type parameters #22056

timholy opened this issue May 25, 2017 · 5 comments

Comments

@timholy
Copy link
Member

timholy commented May 25, 2017

We have some types that have "dependent" type parameters. For instance, in StaticArrays the SArray type is defined

immutable SArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
    data::NTuple{L,T}
end

and an example for a 3x2x5 array might be SArray{Tuple{3,2,5},Float32,3,30}. In all "valid" (aka, useful) instances, N = length(S) and L = prod(S). Consequently it might make sense for the package developer to be able to specify that SArray{S, T} constructs a leaftype, filling in the dependent parameters, rather than a UnionAll type.

Ref JuliaArrays/StaticArrays.jl#181.

@timholy
Copy link
Member Author

timholy commented May 25, 2017

Alternatively one might want to declare the type

immutable SArray{S <: Tuple, T} where {N = length(S), L = prod(S)}  <: StaticArray{S, T, N}
    data::NTuple{L,T}
end

That might be cleaner. Would be good, though, for a method to be able to extract N and L somehow. Maybe

zerotuple(x::SArray{S, T} where {L = prod(S)}) = ntuple(d->zero(T), Val{L})

@timholy
Copy link
Member Author

timholy commented May 25, 2017

xref: this appears to be a potential solution for #5560

@andyferris
Copy link
Member

This also targets a different open issue (I'll try find it later) relating to allowing field types which are (more arbitrary) functions of the type parameters, which would be awesome to solve.

@andyferris
Copy link
Member

xref: it is #18466

@vtjnash
Copy link
Member

vtjnash commented May 25, 2017

already implemented: #18466 (comment)

@vtjnash vtjnash closed this as completed May 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants