[Haskell] Unnamed fields

Ben Rudiak-Gould Benjamin.Rudiak-Gould at cl.cam.ac.uk
Tue Nov 16 12:13:55 EST 2004


Martin Sjögren wrote:

 >On Tue, 16 Nov 2004 15:04:02 +0000, Ian Lynagh <igloo at earth.li> wrote:
 >
 >>Hi all,
 >>
 >>Is there a good reason why I can't say
 >>
 >>    data Bar = Bar { _ :: Int, _ :: Char, x :: Bool }
 >>
 >>?
 >
 >I agree that it would be useful, but wouldn't
 >  data Bar = Bar Int Char { x :: Bool }
 >make more sense as far as syntax goes?

It's a bit problematic because of the current all-at-once behavior of 
labeled construction. One would presumably want to construct a Bar with 
an expression like

    Bar 3 'c' { x = False }

But that won't work because the brace notation has a higher precedence 
than function application (!). You'd have to write

    (Bar 3 'c') { x = False }

I guess you could give Bar the type (Int -> Char -> Bar), with the 
understanding that it initializes x to _|_, and treat the { x = False } 
as an update. But to be consistent, the data constructor in a 
declaration like

    data Quux = Quux { x :: Int, y :: Char, z :: Bool }

ought to have the type Quux then, not (Int -> Char -> Bool -> Quux) as 
it does currently. And none of this is going to work if your labeled 
field is in any position except the last.

-- Ben



More information about the Haskell mailing list