Re: [julia-users] type parametrized in vector length with vector container field

2016-08-05 Thread Erik Schnetter
Pieterjan

Depending on your needs, 
might also be a solution. It supports arrays where the size is known at
compile time.

-erik

On Fri, Aug 5, 2016 at 6:20 AM, Pieterjan Robbe 
wrote:

> Accord to http://docs.julialang.org/en/release-0.4/manual/
> performance-tips/#avoid-fields-with-abstract-containers, this is the way
> to go for types with a container field:
>
> type MyVector{T<:AbstractFloat,V<:AbstractVector}
> v::V
>
> MyVector(v::AbstractVector{T}) = new(v)
> end
> MyVector(v::AbstractVector) = MyVector{eltype(v),typeof(v)}(v)
>
> This is indeed type-stable:
>
> @code_warntype MyVector(0:0.1:1)
>
> However, If I also want to parametrize my type in the vector length L like
>
> type MySecondVector{L,T<:AbstractFloat,V<:AbstractVector}
> v::V
>
> MySecondVector(v::AbstractVector{T}) = new(v)
> end
> MySecondVector(v::AbstractVector) = MySecondVector{length(v),
> eltype(v),typeof(v)}(v)
>
> This is no longer type-stable:
>
> @code_warntype MySecondVector(0:0.1:1)
>
> Can I enforce the length as a parameter so that this is known to the
> compiler or should I look into FixedSizeArrays instead?
>



-- 
Erik Schnetter 
http://www.perimeterinstitute.ca/personal/eschnetter/


[julia-users] type parametrized in vector length with vector container field

2016-08-05 Thread Pieterjan Robbe
Accord 
to 
http://docs.julialang.org/en/release-0.4/manual/performance-tips/#avoid-fields-with-abstract-containers,
 
this is the way to go for types with a container field:

type MyVector{T<:AbstractFloat,V<:AbstractVector}
v::V

MyVector(v::AbstractVector{T}) = new(v)
end
MyVector(v::AbstractVector) = MyVector{eltype(v),typeof(v)}(v)

This is indeed type-stable:

@code_warntype MyVector(0:0.1:1)

However, If I also want to parametrize my type in the vector length L like

type MySecondVector{L,T<:AbstractFloat,V<:AbstractVector}
v::V

MySecondVector(v::AbstractVector{T}) = new(v)
end
MySecondVector(v::AbstractVector) = 
MySecondVector{length(v),eltype(v),typeof(v)}(v)

This is no longer type-stable:

@code_warntype MySecondVector(0:0.1:1)

Can I enforce the length as a parameter so that this is known to the 
compiler or should I look into FixedSizeArrays instead?