3
votes

This is similar to my previous question, but a bit more complicated.

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 to what I want:

type Extp{g::Vector{T}}
     c::Vector{T}
end

In other words, Extp should be defined with respect to a Vector, g, and I want the contents, c, to be another Vector, whose entries should be the of the same type as the entries of g.

Well, this does not work.

Problem 1: I don't think I can use :: in the type parameter.

Problem 2: I could work around that by making the types of g and c arbitary and just making sure the types in the vectors match up in the constructor. But, even if I completely take everything out and use

type Extp{g}
     c
end

it still doesn't seem to like this. When I try to use it the way I want to,

julia> Extp{[1,1,1]}([0,0,1])

ERROR: type: apply_type: in Extp, expected Type{T<:Top}, got Array{Int64,1}

So, does Julia just not like particular Vectors being associated with types? Does what I'm 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, actually, although here it’s clearly used as a type)." Does that mean that what I'm asking is impossible, and that that only types and integers work for Type parameters? If so, why? (what makes integers special over other types in Julia in this way?)

2

2 Answers

2
votes

Here is the relevant quote:

Both abstract and concrete types can be parameterized by other types and by certain other values (currently integers, symbols, bools, and tuples thereof).

So, your EDIT is correct. Widening this has come up on the Julia issues page (e.g., #5102 and #6081 were two related issues I found with some discussion), so this may change in the future - I'm guessing not in v0.4 though. It'd have to be an immutable type really to make any sense, so not Vector. I'm not sure I understand your application, but would a Tuple work?

5
votes

In Julia 0.4, you can use any "bitstype" as a parameter of a type. However, a vector is not a bitstype, so this is not going to work. The closest analog is to use a tuple: for example, (3.2, 1.5) is a perfectly valid type parameter.

In a sense vectors (or any mutable object) are antithetical to types, which cannot change at runtime.