
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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
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
javascript:_e(%7B%7D,'cvml','martin.drautzburg@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@haskell.org javascript:_e(%7B%7D,'cvml','Beginners@haskell.org'); http://www.haskell.org/mailman/listinfo/beginners
-- Sent from an iPhone, please excuse brevity and typos.

I'd agree with Julian Birch, except to say that you shouldn't make a
typeclass unless overloading is really needed and you can define laws on
the behavior thereof.
A fairly major (IMO) inconvenience with embedding functions in records that
have non-function data they operate on is that you won't have a Show
instance. Also, you'll be forced to extract the method and then apply it.
In doing so, to make things nicer you'll probably pull out an "extract and
apply" function. At that point, you're better off lifting the varying
implementations into top-level functions and making a wrapper that decides
which runs where based on the *data* in the record.
That is, including enough information in the record to decide which
function applied.
And this is without having broached whether or not you plan to be able to
serialize your data or not.
On Wed, Sep 10, 2014 at 1:30 PM, Julian Birch
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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Sent from an iPhone, please excuse brevity and typos.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

If the field "label" can be deduced from "payload", I recommend not to
include it in your structure, because that would be redundant.
Here how you could write it:
data Foo pl = Foo { payload :: pl}
labelInt :: Foo Int -> String
labelInt (Foo a) = "Int payload:" ++ (show a)
labelString :: Foo String -> String
labelString (Foo a) = "String payload" ++ a
You are obliged to define two separate label function, because "Foo Int"
and "Foo String" are two completly separate types.
Alternatively, if you want only one label function, use a type sum and
pattern match on it:
data T = TS String | TI Int
data Foo2 = Foo2 { payload2 :: T}
label :: Foo2 -> String
label (Foo2 (TI a)) = "Int payload:" ++ (show a)
label (Foo2 (TS a)) = "String payload:" ++ a
On Wed, Sep 10, 2014 at 8:30 PM, Julian Birch
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
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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Sent from an iPhone, please excuse brevity and typos.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Am 09/10/2014 08:50 PM, schrieb Corentin Dupont:
If the field "label" can be deduced from "payload", I recommend not to include it in your structure, because that would be redundant.
Here how you could write it:
data Foo pl = Foo { payload :: pl}
labelInt :: Foo Int -> String labelInt (Foo a) = "Int payload:" ++ (show a)
labelString :: Foo String -> String labelString (Foo a) = "String payload" ++ a
You are obliged to define two separate label function, because "Foo Int" and "Foo String" are two completly separate types.
This is exactly my problem: Someone will use this type an define the type of pl. How can I know what type she'll use? What I'd like to express is that whoever creates a concrete type should also provide the proper label function.
On Wed, Sep 10, 2014 at 2:06 PM, martin
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
participants (5)
-
Christopher Allen
-
Corentin Dupont
-
David McBride
-
Julian Birch
-
martin