[More work on Chapter 13 John Goerzen **20070815073321] { hunk ./en/ch13-data.xml 197 + + + + Records with Named Fields + + Our earlier example of a type that holds a color looked easy enough. + But you have to remember the ordering of the fields that are part of + the type. Also, you always have to pattern match all fields, even if + you're interested in only one. Of course, you could write a function + such as getRed to do that, but there's an easier + way. + + + In Haskell, records can have named fields. When you name the fields + in a record, you can still access it just as you would without the + named fields. But you gain two things: automatic functions for + picking out specific fields, plus an easier way to create and update + these objects. + + + Let's take a look at a re-designed CustomColor + type that uses named fields: + + &colornamed.hs:custom; + + This record stores exactly the same amount of information as our + ealier CustomColor. But now we can take advantage + of named fields. Let's see how that works with &ghci;: + + &colornamed.ghci:custom; + &colornamed.ghci:create; + + First, we inspected the type of the CustomColor + constructor. Note that it's type is exactly the same as the + constructor that didn't use named fields. Then, we created a object + with identical data three different ways. The first way didn't make + use of the named fields. The second and third ways did. Notice that + when you use named fields, you don't have to specify the values in + order. + + + Haskell automatically creates accessor functions for each of the + named fields. Let's look at how we extract the red component of our + color: + + &colornamed.ghci:extract; + + We can use that to write a modified color2string + function that accesses each named field without having to pattern + match each individual field. For reference, the original + color2string is included here as + color2string2. + + &colornamed.hs:extract; + &colornamed.hs:extract2; + + In the first function, we took a CustomColor and + assigned the entire thing to cc. In the second + function, we used pattern matching to pick it apart up front. + + + Named fields also make it easy to modify one or more components of a + type. Here's an example: + + &colornamed.ghci:update; + + With just three fields, the burden of pattern matching isn't all that + great. But what if you had a dozen or more? Named fields really + help out in that case. The standard Haskell library uses named + fields in System.Time.CalendarTime. Here's a + excerpt from its definition: + + +data CalendarTime = CalendarTime { + ctYear :: Int + ctMonth :: Month + ctDay :: Int + ctHour :: Int + ctMin :: Int + ctSec :: Int + ctPicosec :: Integer + ctWDay :: Day + ctYDay :: Int + ctTZName :: String + ctTZ :: Int + ctIsDST :: Bool +} + + + Named fields are a tremendous time saver here. If you have a + CalendarTime and want to extract just the year, + you can simply say ctYear ct rather than having to + match against 12 different fields in order. + + + + Polymorphic Types + + asdf + }