[Haskell-cafe] Get number of fields for non-record ADTs using Data type class?
Artem Pelenitsyn
a.pelenitsyn at gmail.com
Mon Oct 29 20:42:16 UTC 2018
I don't think there is a point in looking for GHC.Generics-based solution,
as Data.Data is the exact match for this kind of problem.
--
Best, Artem
On Mon, 29 Oct 2018 at 16:35 Li-yao Xia <lysxia at gmail.com> wrote:
> This maps every field to 1, and folds them together using (+):
>
> Data.Data.gmapQl (+) 0 (const 1) :: T -> Int
>
> (There has to be a similarly easy solution using GHC.Generics instead
> but I can't think of one...)
>
> Li-yao
>
> On 10/29/18 2:56 PM, Markus Läll wrote:
> > Dear list,
> >
> > Is it possible te get the number of fields for data constructors for a
> > plain ADT, i.e something with no record fields? E.g for
> >
> > data T = A Int Double | B String (Maybe String)
> >
> > it would give 2 for both `A` and `B`.
> >
> > For a record it's possible using the `constrFields` function from
> Data.Data.
> >
> > I was trying to follow this tutorial by Christopher Done
> > https://chrisdone.com/posts/data-typeable, and I feel that it must be
> > possible somehow to get these numbers with the gmap*/gfold* functions,
> > but the use of them is over my head at the moment.
> >
> >
> > Best,
> >
> >
> >
> > --
> > Markus Läll
> >
> > _______________________________________________
> > 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.
> >
> _______________________________________________
> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20181029/9bd39198/attachment.html>
More information about the Haskell-Cafe
mailing list