
If I use an STRef to a record, will a new record be created each time I want to update a single field? Or can I expect GHC to optimize it and have the field of the record updated in place?
You'll get a new record for each update. This might not be so bad though, depending on the number of fields in your record. One of them has 4, the other 3, but they might grow bigger...
Here's another trick if you use this route: <added some parenthesis> data E s = E{ refi :: !(STRef s Int), refc :: !(STRef s Char) }
and compile with -funbox-strict-fields. This will eliminate the boxing of the STRefs.
Nice, thanks :) One more question. I'm passing that 'record' around as an implicit value. The record as STRefs that I use to collect info, but it also has some pure fields with 'read-only' info. Something like, data E s = E{ refi :: STRef s Int, refc :: STRef s Char, max :: Int } In some functions I might need only some pure fields, and none of the STRefs, but since I pass something of type 'E s' around, everything ends up beeing monadic. Is using two records (on with STRefs and one with pure fields) the only/best way to avoid this? I would like to use two records, doesn't seem natural, but I also don't like to end up using monadic functions when they are, in fact, pure... Thanks, J.A.