[Haskell] Unnamed fields

Ian Lynagh igloo at earth.li
Tue Nov 16 16:10:01 EST 2004


On Tue, Nov 16, 2004 at 04:07:48PM +0000, Malcolm Wallace wrote:
> On Tue, 16 Nov 2004 15:04:02 +0000, Ian Lynagh <igloo at earth.li> wrote:
> 
> > > Is there a good reason why I can't say
> > > 
> > >     data Bar = Bar { _ :: Int, _ :: Char, x :: Bool }

In case it wasn't clear, there is an x :: Bool in lots of alternatives,
so I really want

data Bar = Bar { _ :: Int, _ :: Char, x :: Bool }
         | Baz { _ :: String, x :: Bool }
         | ...

> Since you only want one field out of many, what is the difficulty in
> simply defining the projection/updating functions separately?
>
>     data Bar = Bar Int Char Bool
>     x (Bar _ _ b) = b
>     (Bar i c _) `updX` b = Bar i c b

Then I have to write twice as much again (2n if there are n fields I do
want to name). I'm not denying it's possible to do without it, but it
would make life easier if we had the above. I also think that it feels
inconsistent that it isn't possible.


Thanks
Ian



More information about the Haskell mailing list