Make it possible to evaluate monadic actions when assigning record fields

kahl at cas.mcmaster.ca kahl at cas.mcmaster.ca
Tue Jul 10 13:04:47 EDT 2007


Isaac Dupree <isaacdupree at charter.net> wrote:
 > 
 > Adde wrote:
 > >  tmp <- foo
 > >  return Bar {
 > >    barFoo = tmp
 > >  }
 > 
 > There is a feature being worked on in GHC HEAD that would let you do
 > 
 >   do
 >    tmp <- foo
 >    return Bar{..}
 > 
 > which captures fields from everything of the same name that's in scope. 
 >   I think this would also satisfy your desire.
 > 

I guess this means I could write:


data D = C {field1 :: Bool, field2 :: Char}

f x = do
  field1 <- foo1
  field2 <- foo2
  field3 <- foo3
  other stuff
  return C{..}


instead of


f x = do
  tmp1 <- foo1
  tmp2 <- foo2
  field3 <- foo3
  other stuff
  return $ C { field1 = tmp1, field2 = tmp2 }


This has a dangerous feel to it ---
extending the definition of D to include a field field3
may have quite unintended consequences.


What I am missing most in the record arena
is a functional notation for record update, for example:

{^ field1 }  =  \ f r -> r {field1 = f (field1 r)}


More information about the Haskell-prime mailing list