[Haskell-cafe] Is it possible to get the selector functions when defining a generic class?
David Feuer
david.feuer at gmail.com
Thu Nov 24 03:06:32 UTC 2016
Sorry if anyone gets this twice; the first copy somehow went to a
non-existent Google Groups version of haskell-cafe.
GHC.Generics doesn't offer any built-in support for such things. It
*looks* like there *might* be some support in packages built around
generics-sop. When you're working directly with GHC.Generics, the
notion of a record barely even makes sense. A record is seen as simply
a possibly-nested product. For example, ('a','b','c') will look
*approximately* like 'a' :*: ('b' :*: 'c'). You're generally not
"supposed" to care how large a record you may be dealing with, let
alone what field names it has. May I ask what you're actually trying
to do? Your specific request sounds peculiarly un-generic.
On Wed, Nov 23, 2016 at 9:52 PM, <chris at kahn.pro> wrote:
> Hey all!
>
> I'm trying to understand what's going on in GHC.Generics and defining a
> generic class... I understand that there's a `Selector` class and `selName`
> function that can get the name of a selector, but is there a way to access
> the selector function itself? The documentation conveniently avoids examples
> involving records and is otherwise quite barren.
>
> So if I have a data type like...
>
> data Person = Person
> { name :: String
> , age :: Int
> } deriving Generic
>
> instance MyTypeClass Person
>
> I want my generic implementation of MyTypeClass to be able to access each
> selector function in the record, f :: Person -> String, g :: Person -> Int,
> etc.
>
> Chris
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list