Open chris-martin opened 4 years ago
Since I really like the idea of introducing optics as if it was something super simply and approachable and wanted to check out that new package anyway I'll get on it :100:
Lately I'm feeling like the Phrasebook should just immediately introduce optics from the start.
If that is a road you want to go down, I'd recommend taking a hard look at going the path of generic-lens (https://hackage.haskell.org/package/generic-lens) and generic-lens-labels (http://hackage.haskell.org/package/generic-lens-labels) rather than that of makeLenses
. With the two above-mentioned packages, you get to avoid any magic strings in the names of your record fields. Access and manipulation looks like view #name person
and over #name (map toUpper) person
, respectively. Slightly annoyingly, you need to import the two libraries and set XOverloadedLabels
in each module where you want to use this syntax, though, which maybe outweighs any potential gain.
I'm a big fan of generic-lens
, but I can see why you might not want to use it. In your case, it's not an engineering trade-off and a pedagogical trade-off.
Either way you want to get your lenses, records with optics up front is--I think--the right way to go. Using optics to modify a deeply-nested field is a way in which Haskell records are genuinely, objectively better than imperative languages. E.g. in imperative code, you can use +=
to apply a (terribly specific) function to a deeply-nested field (of a very specific type), whereas over
lets you do that for any function at any type! I think you should showcase that :-)
Here's a records demo we wrote a while back:
Lately I'm feeling like the Phrasebook should just immediately introduce optics from the start.
Maybe that's too radical. On the other hand, maybe introducing it now in an extremely simple context is good setup for a later page on doing "deep updates" with composed lenses. I think in a later page we could end up showing how to get a lot of of optics using only
view
,set
,over
, and(%)
without being overwhelming.