How to use a vector as a type parameter in Julia
This is similar to my previous question , but a little more complex.
Before I was defining a type with an associated integer as a parameter, Intp {p}. Now I would like to define a type using a vector as a parameter.
The following is the closest I can write what I want:
type Extp{g::Vector{T}}
c::Vector{T}
end
In other words, Extp must be defined relative to a vector, g, and I want the content of c to be another vector whose entries must be of the same type as the g entries.
Well, it won't work.
Problem 1: I don't think I can use :: in the type parameter.
Problem 2: I could get around this by creating types g and c arbitary and just making sure the types in the vectors are the same in the constructor. But, even if I completely select everything and use
type Extp{g}
c
end
he doesn't like it anyway. When I try to use it the way I want,
julia> Extp {[1,1,1]} ([0,0,1])
ERROR: type: apply_type: in Extp, expected type {T <: Top}, got array {Int64,1}
So isn't Julia just looking like the specific Vectors associated with types? Does what I am trying to do only work with integers like in my Intp question?
EDIT: In the documentation, I see that type parameters "can be any type at all (or an integer, in fact, although it is explicitly used as a type here)". Does this mean that what I am asking is not possible and that only types and integers work for type parameters? If so, why? (what does integers do this way compared to other types in Julia?)
source to share
Here's a relevant quote :
Both abstract and concrete types can be parameterized with other types and some other values (currently integers, characters, bools and tuples).
So your EDIT is correct. An extension of this issue showed up on the Julia issue page (for example # 5102 and # 6081 were two related issues that I found with some discussion), so this may change in the future - I'm guessing not v0.4
. It should be an immutable type that actually makes sense, not Vector
. I'm not sure I understand your application, but does it work Tuple
?
source to share
In Julia 0.4, you can use any "bitstype" as the type parameter. However, the vector is not a bitstring, so this won't work. The closest analogue is using a tuple: for example, it (3.2, 1.5)
is a perfectly valid type parameter.
In a sense, vectors (or any mutable object) are the opposite of types, which cannot change at runtime.
source to share