Thanks everyone for your helpful suggestions. Although a few details seem 
fuzzy, I think I am learning quite a bit and I am very close to the final 
product. 

Combining  Ford's initial code, Steve's code and David's code allowed me to 
access the information from specific individuals (e.g. population[1]) and 
access information across individuals (e.g. population[:infected] ). One 
thing is perplexing to me. I cannot change the values once the have been 
created. For example, after running the code below, I get:

population[:infected][1]

true


population[:infected][1] = falsepopulation[:infected][1]


true


That seems odd, but obviously I am missing something. The other question I have 
is whether my naive combining of code is the best implementation?







Combined code: 

type Person
    infected::Bool
    vaccinated::Bool
    dead::Bool
    history::Vector{Int}
end

type Population <: AbstractArray
    individuals::Vector{Person}
    
end


Population() = Population(Person[])
Base.push!(p::Population, x::Person) = push!(p.individuals, x)
length(p::Population) = length(p.individuals)
size(p::Population) = size(p.individuals)

function Base.getindex(pop::Population, field::Symbol)
    [getfield(x, field) for x in pop.individuals]
end

Base.setindex!(p::Population, i::Person, index...) = 
p.individuals[index...] = i
Base.getindex(p::Population, index) = p.individuals[index]
infected(i::Person) = i.infected
vaccinated(i::Person) = i.vaccinated

Base.show(io::IO, p::Person) = print(io, "Person(infected=", p.infected, ", 
vaccinated=", p.vaccinated, ", dead=", p.dead, ", history=", p.history)


population = Population()
push!(population, Person(true, false, true, [3,4,5]))
push!(population, Person(false, true, false, Int64[3,4,5]))

#Show that it works
population[:infected]
population[2]


Reply via email to