Thanks for the interesting suggestion.
I will have to experiment with that. FWIW, this is the python code I am trying to “replicate”: class DiscreteRV(object): """ Generates an array of draws from a discrete random variable with vector of probabilities given by q. """ def __init__(self, q): self._q = q self.Q = cumsum(q) def get_q(self): return self._q def set_q(self, val): self._q = val self.Q = cumsum(val) q = property(get_q, set_q) On Thursday, July 10, 2014 2:35:45 PM UTC-4, Tim Holy wrote: If you're in the consenting-adults camp, overloading setindex! would be > fine. > Modulo bounds, error-checking, etc, it's as simple as > > function setindex!(rv::DiscreteRv, val, indx) > rv.q[indx] = val > for j = indx:length(rv.Q) > rv.Q[j] = rv.Q[j-1]+rv.q[j] > end > rv > end > > If you want to make it "impossible" to change q without updating Q, then > you've got a much harder problem on your hands. If a language allows you > to > take a pointer to an object (which Julia does, for C interop), then > there's > almost nothing you can guarantee. > > --Tim > > On Thursday, July 10, 2014 10:38:40 AM Spencer Lyon wrote: > > So I read through issue 1974 > > <https://github.com/JuliaLang/julia/issues/1974>. It seems like what I > am > > trying to do is not possible before that gets implemented. Is that > correct? > > > > On Thursday, July 10, 2014 1:15:14 AM UTC-4, Spencer Lyon wrote: > > > > I have a very simple composite type: > > > type DiscreteRv{T <: Real} > > > > > > q::Vector{T} > > > Q::Vector{T} > > > > > > end > > > > > > DiscreteRv{T <: Real}(x::Vector{T}) = DiscreteRv(x, cumsum(x)) > > > > > > Here q represents a probability vector for a discrete random variable. > Q > > > is the associated cdf. > > > > > > I want to make sure that the relationship Q = cumsum(q) is preserved > > > whenver q is updated. > > > > > > I presume I need to overload Base.setindex!, but I haven’t been able > to > > > figure it out. Any suggestions? > > > ------------------------------ > > > > > > In the end I would like this example to work: > > > > > > julia> d = DiscreteRv([.1, .9]) > > > DiscreteRv{Float64}([0.1,0.9],[0.1,1.0]) > > > > > > julia> d.q = [.3, .7]; d > > > DiscreteRv{Float64}([0.3,0.7],[0.3,1.0]) > > > > > > > > > > > >