[Haskell-beginners] Can fields in a record be optional?

austin seipp as at hacks.yi.org
Mon Jul 18 23:33:49 CEST 2011


It's effectively the exact same thing as your example but with the
default record put into a typeclass for convenience. Refactoring your
original post:

data Contract =  Contract {
                   currency  :: Currency
                 , payments  :: Double
                 , contracts :: [Contract]
               }
               deriving (Show)

instance Default Contract where
  def = Contract { currency = undefined, payments = undefined, contracts = [] }

one    :: Currency -> Contract
one c  =  def { currency = c, payments = 1}

and          :: Contract -> Contract -> Contract
(and) c1 c2  =  def { contracts = [c1, c2] }

It's a very simple package and provides some default instances for
your convenience. Use it if you like.

On Mon, Jul 18, 2011 at 3:47 PM, David Place <d at vidplace.com> wrote:
> Thanks for the pointer to Data.Default.  I don't see how it applies in this case, though.  The goal is to avoid mentioning the defaulted fields when constructing a record instance.  Example?
>
> ____________________
> David Place
> Owner, Panpipes Ho! LLC
> http://panpipesho.com
> d at vidplace.com
>
>
>
> On Jul 18, 2011, at 1:45 AM, Christopher Done wrote:
>
>> On 18 July 2011 00:18, David Place <d at vidplace.com> wrote:
>>> The way I often do this is to create an "ur" instance where all the fields have default values.  Then to create an instance, I just do a "record update" of this instance.
>>
>> For this there is Data.Default in data-default:
>>
>> http://hackage.haskell.org/packages/archive/data-default/0.2.0.1/doc/html/Data-Default.html
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Regards,
Austin



More information about the Beginners mailing list