Record of STRefs better than STRef to a Record?

Jorge Adriano
Wed, 13 Nov 2002 23:29:18 +0000

> > 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 =3D 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.=20
I'm passing that 'record' around as an implicit value. The record as STRe=
that I use to collect info, but it also has some pure fields with 'read-o=
info. Something like,

data E s =3D 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 STR=
but since I pass something of type 'E s' around, everything ends up beein=
monadic. Is using two records (on with STRefs and one with pure fields) t=
only/best way to avoid this?=20
I would like to use two records, doesn't seem natural, but I also don't l=
to end up using monadic functions when they are, in fact, pure...