<div dir="ltr">Hi Brent and Brandon,<br><br>Thank you for responding! Okay, so from the responses it seems that it&#39;s simple to define a &quot;datatype&quot; (partially applied function, really) which has &quot;default&quot; parameters for all fields. Or, using the same method, for the first N fields (N is less than or equal to the number of fields):<br>
<br><div style="margin-left: 40px;">defaultCust = Customer 0 &quot;&quot; -- for instance, building upon Brent&#39;s example<br></div><br>However, it&#39;s not quite so easy to pick and choose (define only the name, for instance). I guess this really isn&#39;t too much of a limitation, since a simple function could set any defaults you want:<br>
<br><div style="margin-left: 40px;">defaultCust2 id addr = Customer id &quot;Bill&quot; addr -- function which simulates a default value for &quot;name&quot;<br></div><br>So despite the lack of syntactic sugar, the simplicity and power of functions can make do. One down side, however, is that you lose the flexibility of record syntax (unless there is an analogue for functions that I don&#39;t know about).<br>
<br>Mike<br><br><br><br><br><div class="gmail_quote">On Tue, Sep 23, 2008 at 11:25 PM, Brandon S. Allbery KF8NH <span dir="ltr">&lt;<a href="mailto:allbery@ece.cmu.edu">allbery@ece.cmu.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div style=""><div><div></div><div class="Wj3C7c"><div><div>On 2008 Sep 22, at 15:38, Mike Sullivan wrote:</div><blockquote type="cite"><div><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;"><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;">
<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=&quot;Bill&quot;}<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></div><div>aCustomer = Customer { customerAddress = Nothing }</div>
<div>-- ...</div><div>a = aCustomer { customerID = 12, customerName = &quot;Bill&quot; }</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>&nbsp;&nbsp; &nbsp;*Main&gt; customerName b</div><div>&nbsp;&nbsp; &nbsp;&quot;*** Exception: fooo.hs:6:12-49: Missing field in record construction Main.customerName</div><div><br></div><div>You will *not* get &nbsp;a warning for missing values in variables initialized this way, only for the special initializer value. &nbsp;I&#39;m not sure if it&#39;s possible to make the type system handle this, but if it is then it&#39;ll probably be painful and ugly. &nbsp;(Now watch someone post an elegant one-liner....)</div>
</div><div><br></div><div><span style="font-family: Monaco; font-size: 11px;">--&nbsp;</span></div><div><span style="border-collapse: separate; border-spacing: 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-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="">
<span style="border-collapse: separate; border-spacing: 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-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><span style="border-collapse: separate; border-spacing: 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-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div>
<font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">brandon s. allbery [solaris,freebsd,perl,pugs,haskell] <a href="mailto:allbery@kf8nh.com" target="_blank">allbery@kf8nh.com</a></span></span></font></div>
<div><font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">system administrator [openafs,heimdal,too many hats] <a href="mailto:allbery@ece.cmu.edu" target="_blank">allbery@ece.cmu.edu</a></span></span></font></div>
<div><font face="Monaco"><span style="font-family: Monaco;"><span style="font-family: Monaco;">electrical and computer engineering, carnegie mellon university &nbsp; &nbsp;KF8NH</span></span></font></div><span style="border-collapse: separate; border-spacing: 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-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><br>
</span></span></span></div></span> </div><br></div></blockquote></div><br></div>