f# - Completely lost in trying to mutate property in sequence -
i @ loss why code doesn't mutate member variable in sequence of types:
for p in prescrs p.atc <- "a" c in p.drug.components s in c.substances s.dosetotal.adjust <- adjustkg s.dosetotal.time <- "day" s.dosetotal.unit <- s.drugconcentration.unit s.doserate.adjust <- adjustkg s.doserate.time <- "day" s.doserate.unit <- s.drugconcentration.unit
prescrs sequence of prescriptions simple 'poco' defined type member values. don't have clue why doesn't work.
i tried simple test case like:
type itertest () = member val name = "" get, set member val itertests = [] |> list.toseq : itertest seq get, set let iterseq = [ new itertest(name = "test1") new itertest(name = "test2") ] |> list.toseq iterseq |> seq.iter(fun x -> x.itertests <- iterseq) iterseq |> seq.iter(fun x -> x.itertests |> seq.iter(fun x' -> x'.name <- "itered"))
but here result expected. so, can't quite reproduce problem???
found solution (without understanding problem above). when first convert prescrs sequence list like:
let prescrs = prescrs |> seq.tolist
and imperative looping, properties mutated.
try sample:
type mutable() = member val iterated = false get, set let muts = seq.init 5 (fun _ -> printfn "init"; mutable()) let muts2 = muts // try again let muts2 = muts |> list.ofseq printfn "before iter" in muts2 printfn "iter" a.iterated <- true printfn "after iter" muts2 |> list.ofseq
and check how iter
, init
interleaved.
seqs
lazy, not cached once computed. if imperatively try mutate of elements in prescrs
sequence, goes away once pull prescrs
again. if change prescrs
concrete collection type list before doing mutation, no longer hit same problem. note things might trickier if have seq inside seq inside seq.
the best idea avoid mutation in first place though.
Comments
Post a Comment