[Haskell-beginners] How to add a "method" to a record

Julian Birch julian.birch at gmail.com
Wed Sep 10 18:30:16 UTC 2014


Bear in mind you can just create functions at the top level that operate on
your data structure.  You only need the function to be a member of the
record if the function itself changes, which is relatively rare.

Say you need a Foo and a Bar, and they both have labels, implemented in
different ways, then you can use a typeclass to achieve your goals.

On Wednesday, September 10, 2014, David McBride <toad3k at gmail.com> wrote:

> Is this what you are looking for?
>
> data Foo pl = Foo {
>   label :: pl -> String,
>   payload :: pl
> }
>
> On Wed, Sep 10, 2014 at 2:06 PM, martin <martin.drautzburg at web.de
> <javascript:_e(%7B%7D,'cvml','martin.drautzburg at web.de');>> wrote:
>
>> Hello all
>>
>> if I have a record like
>>
>>         data Foo pl = Foo {
>>                     label :: String,
>>                     payload :: pl
>>                 }
>>
>> how can I create a similar type where I can populate label so it is not a
>> plain string, but a function which operates on
>> payload? Something like
>>
>>         label (Foo pl) = show pl
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> <javascript:_e(%7B%7D,'cvml','Beginners at haskell.org');>
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>

-- 
Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140910/9592b852/attachment.html>


More information about the Beginners mailing list