Making full use of the Generic type class

I just published a blog post whose goal is to teach you how to write the code which permits Generic deriving for a particular type class:

https://harry.garrood.me/blog/write-your-own-generics/

Let me know what you think!

14 Likes

If I haven’t seen generics in action yet, where to look first? What’s the best place where you’d direct me to?

I like how the imports are used as a visual element to add some space between the preface here. If you got some literate programs like these, it’s a great idea to use it that way.

The link to try purescript here is also appreciated, I think I’ll try the code samples later on. This is giving me an idea though. Could it be used directly in this kind of posts to bring the code alive? That’s something to think of at a coffee break.

1 Like

Thanks for the great article, Harry! Very helpful for a beginner to generics like me.

Between this and the classic Type Families and Pokémon article, there really should be a warning somewhere that Pokémon is pre-requisite knowledge for getting into strongly-typed functional programming. :sweat_smile:

3 Likes

Module docs: https://pursuit.purescript.org/packages/purescript-generics-rep
JSON encoding / decoding use case: https://github.com/purescript-contrib/purescript-argonaut-codecs/tree/master/docs#generics

1 Like

Yep, I think it could - the API provided by the Try PureScript backend isn’t particularly complicated. You’d probably need to go digging around in the Try PureScript source code a little bit to see how it works. For this particular post I’m not sure how helpful that would be, since there isn’t much to run and see the output of until everything has been put together, but I can definitely see that being useful in other places.

lol - I wasn’t aware of that article until after I wrote this one, I swear!

2 Likes

The last paragraph points to argonaut-codecs, but argonaut-generic defines genericEncodeJson / genericDecodeJson. Did you intend to point to the latter package instead?

Syntax highlighting for the code snippets would be a nice blog enhancement.

Yes, I did intend to point to argonaut-generic, thanks.