But if you're not actually going to instantiate such a humongous number of them, or if you really want to be able to use specialization and dispatch in this way: How about a middle road where you use a parametric type and set the types of all unused fields to Void (the type of nothing)? That should be able to support the cases that you mentioned.
Also, in many cases, storage for a value worth known type of eg Void is free, since it is known that there is only one instance. The exception is if the value could be uninitialized as well.
If in contrast item[i+1] has a different type than item[i], and the amount of
processing is quite modest, then it may not be worth it. Because julia can't
predict the type at compile-time, it has to look up the type at run-time,
search for the appropriate method in method tables, decide (via type
intersection) which one matches, determine whether it has been JIT-compiled
yet (and do so if not), and then make the call. You're asking the full type-
system and JIT-compilation machinery to basically execute the equivalent of a
switch statement or dictionary lookup in your own code. Julia can do this, but
it's a lot of churn under the hood. If this is the situation you're in, it
seems likely to be better to just write that switch statement or to use a
dictionary.
typealias Color ASCIIString
typealias Horsepower Float64
typealias Model ASCIIString
typealias Year Int
type Car{
C<:Union{Color,Void},
H<:Union{Horsepower,Void},
M<:Union{Model,Void},
Y<:Union{Year,Void}
}
color::C
horsepower::H
model::M
year::Y
end
# outer constructor
function Car(; color=nothing, horsepower=nothing, model=nothing, year=nothing)
Car(color, horsepower, model, year)
end
# create cars by use keyword arguments in any order
myoldcar = Car(year=1967, horsepower=450.0, color="Red")
mynewcar = Car(year=2016, color="White")
# function operates on any homogeneous garage of cars with color and year defined
function Count_Red_1963{C<:Color,H,M,Y<:Year}(garage::Vector{Car{C,H,M,Y}})
count = 0
for car in garage
if car.color == "Red" && car.year == 1963
count += 1
end
end
return count
end
garage_color_model_year = [ Car(color="Red", model="GTX", year=1963) for i = 1:10 ]
# TODO: work out how to avoid messy conversion
garage_color_model_year = convert(Vector{typeof(garage_color_model_year[1])}, garage_color_model_year)
garage_color_year = [ Car(color="Red", year=y) for y = 1960:1969 ]
garage_color_year = convert(Vector{typeof(garage_color_year[1])}, garage_color_year)
Count_Red_1963(garage_color_model_year)
Count_Red_1963(garage_color_year)