<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On 2008 Sep 22, at 15:38, Mike Sullivan wrote:</div><blockquote type="cite"><div dir="ltr"><font style="font-family: arial,helvetica,sans-serif;" size="2">I was wondering if there is syntactic sugar in Haskell for defining a default value for fields in a data type. For instance, say I have a type that is defined in record syntax:<br> </font><pre style="margin-left: 40px; font-family: arial,helvetica,sans-serif;" id="BookStore.hs:BetterReview" class="programlisting"><font size="2">type CustomerID = Int<br>type Address = Maybe String<br></font></pre><div style="margin-left: 40px; font-family: arial,helvetica,sans-serif;"> <font size="2">data Customer = Customer {<br> customerID :: CustomerID<br> , customerName :: String<br> , customerAddress :: Address<br> } deriving (Show)<br></font></div><pre style="font-family: arial,helvetica,sans-serif;" id="BookStore.hs:Customer" class="programlisting"><font size="2">Is there any way to define default values for some (or all) fields such that they may be omitted from a declaration, and still have it generate a valid object?<br><br></font></pre><div style="margin-left: 40px; font-family: arial,helvetica,sans-serif;"> <font size="2">e.g.) <br>a = Customer{customerID = 12, customerName="Bill"}<br>-- I would like a{customerAddress} to default to Nothing (for instance).<br></font></div><div style="font-family: arial,helvetica,sans-serif;"> <br>It seems to me that this would be a nice feature to have, if it does not exist. Am I missing something?<br></div></div></blockquote></div><div><br></div><div>aCustomer = Customer { customerAddress = Nothing }</div><div>-- ...</div><div>a = aCustomer { customerID = 12, customerName = "Bill" }</div><div><br></div><div>This will net you a compile time warning about uninitialized fields in aCustomer, and if you fail to initialize a Customer properly it will produce a runtime error:</div><div><br></div><div><div> *Main> customerName b</div><div> "*** Exception: fooo.hs:6:12-49: Missing field in record construction Main.customerName</div><div><br></div><div>You will *not* get a warning for missing values in variables initialized this way, only for the special initializer value. I'm not sure if it's possible to make the type system handle this, but if it is then it'll probably be painful and ugly. (Now watch someone post an elegant one-liner....)</div></div><div><br></div><div><span class="Apple-style-span" style="font-family: Monaco; font-size: 11px; ">-- </span></div><div apple-content-edited="true"><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">brandon s. allbery [solaris,freebsd,perl,pugs,haskell] <a href="mailto:allbery@kf8nh.com">allbery@kf8nh.com</a></span></span></font></div><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">system administrator [openafs,heimdal,too many hats] <a href="mailto:allbery@ece.cmu.edu">allbery@ece.cmu.edu</a></span></span></font></div><div><font class="Apple-style-span" face="Monaco"><span class="Apple-style-span" style="font-family: Monaco; "><span class="Apple-style-span" style="font-family: Monaco; ">electrical and computer engineering, carnegie mellon university KF8NH</span></span></font></div><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 11px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><br class="Apple-interchange-newline"></span></span></span></div></span> </div><br></body></html>