I'm playing with defining Linked Lists in Julia. The version I'm struggling
with involves having a type parameter on the node and list, that indicates
the type of the data in the list.

These are the types:
~~~
type List{T}
   head::Union(Node{T},Nothing)
end

type Node{T}
   data::T
   next::Union(Node{T},Nothing)
end
~~~

In order to add data to List, I'm implementing unshift!. My first version
worked pretty well:
~~~
function Base.unshift!{T}(l::List{T},item::T)
         new_node = Node{T}(item,l.head)
         l.head = new_node
         return l
end
~~~

However, when I make a List{String} and try to put an ASCIIString into it,
things go poorly:
~~~
julia> ll = List{String}(nothing)
[]

julia> unshift!(ll, "hello")
ERROR: no method unshift!(List{String}, ASCIIString)

julia> super(ASCIIString)
DirectIndexString

julia> super(ans)
String
~~~

I want to make unshift! work for this case. My next attempt results in an
error, so I'm not sure what to do:
~~~
julia> function unshift!{T,I<:T}(l::List{T},item::I)
         new_node = Node{T}(item,l.head)
         l.head = new_node
         return l
       end

ERROR: T not defined
~~~

This doesn't make sense to me. T is the first type variable that I declare.
If it's complaining about "I<:T", then shouldn't T already be defined by
then?

thanks,
Leah

Reply via email to