Make it possible to evaluate monadic actions when assigning
record fields
Adde
adde at trialcode.com
Tue Jul 10 16:40:12 EDT 2007
On Tue, 2007-07-10 at 17:04 +0000, kahl at cas.mcmaster.ca wrote:
> 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)}
I agree, capturing variables without asking is just scary.
While I'm pretty biased I still think my suggestion solves the problem
in a cleaner, more consistent way.
/Adde
More information about the Haskell-prime
mailing list