我宣布了自己的类型:
data Book = Bookinfo {
bookId :: Int,
title :: String
} deriving(Show)
现在:
x = Bookinfo
一切都好,有效的陈述
但是使bookId x抛出错误。
如果我能够处理Haskell中的错误就可以了但是现在我无法做到这一点我很好奇如何使未指定的字段值采用默认值,当我不是时,究竟有什么价值在construcotr中提供字段的vcalues?
感谢您的帮助
答案 0 :(得分: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" }