5

I have declared my own type:

data Book = Bookinfo {
              bookId :: Int,
              title :: String
          } deriving(Show)

and now:

x = Bookinfo

it is all ok, valid statement

but making bookId x throws an error.

If I would be able to handle errors in Haskell that would be ok but right now I cant do this So Im curious how to make not specified values of fields take default value, and what exactly value is there when I'm not giving vcalues of fields in construcotr ?

thanks for help

Norman Ramsey
  • 188,173
  • 57
  • 343
  • 523
gruber
  • 24,755
  • 31
  • 114
  • 208
  • 1
    Similar question: http://stackoverflow.com/questions/7781096/is-there-a-better-way-to-have-optional-arguments-in-haskell – MatrixFrog Oct 15 '11 at 22:47

1 Answers1

12
-- this one is not a Book but actually a function that can make one:
alternativeCtrFunc = Bookinfo

defaultBook = Bookinfo { bookId = 3, title = "Welcome to the Monkey House" }

x = defaultBook
y = defaultBook { bookId = 7 }
z = defaultBook { title = "The Cider House Rules" }
yairchu
  • 21,122
  • 7
  • 65
  • 104