Chapter 6 - Purescript By Example - excercise

I’m new here, so hello to everyone :slight_smile:

I’ve been slowly going through the book and found myself confused by one of the excercises.

The only module I found (that seems to be fit for purpose) is ‘Data.Generic.Rep’.
I’ve tried using ‘from’ method, but the output does not match expected test results.

My code:

derive instance genericShape :: Generic Shape _

instance showShape :: Show Shape where
  show a = show $ from a

Test output:

Am I even using the right ‘Generic’ module, and if yes, how would I go about reformatting the result?

Generic is never used directly to accomplish this task. Rather, it’s primarily for library authors. You as an end-user would use the library written by said authors to accomplish your task.

See this explanation: https://github.com/JordanMartinez/purescript-jordans-reference/blob/latestRelease/31-Design-Patterns/22-Generics.md

1 Like

The book should probably just tell you how to do this rather than expecting you to figure it out; figuring it out yourself is likely to be frustrating and not very enlightening, as at this stage you’re probably not ready to understand the Generic mechanism or what the type of genericShow means. Knowing how to get the compiler to write a Show instance for you feels more like arbitrary rather than necessary knowledge to me.

4 Likes

The previous section in the book points to the Type Class Deriving guide, which has a section on Deriving from Generic. You should be able to pretty much just copy and paste the example in that guide.

Here’s a PR to make that exercise clearer.

Thanks for bringing this to our attention.

2 Likes

Excellent! Thank you all for your answers.
I’ve seen the guide, but must have missed the section on Generics.
After that I would just search the book when trying to understand the requirements, so another reminder about the link, inside the exercise, seems like a good idea.

All tests are passing now :partying_face:

It might be worth mentioning that ‘Data.Generic.Rep.Show’ used in the example is now deprecated.
I found what I needed inside ‘Data.Show.Generic’ though, when searching for ‘genericShow’ in Pursuit.

1 Like

Yeah, we need to get this other PR merged, which includes that correction:

1 Like