-
-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic Product Field Traversals #145
Comments
I think this is a good idea! There's no nice alternative that I'm aware of that achieves this goal in a satisfactory way. A lot of the lens derivation code can be piggybacked off of, so implementation-wise I don't expect this to require too much work. I would most certainly welcome a PR! |
I agree that this makes a lot of sense to offer. FWIW |
An alternative that I would like to see, but am entirely incapable of judging whether it's (easily) possible: Can we not leverage some machinery to make it so that the type returned by using a ctor prism actually does have the fields that it "should"? The ones that come to mind are:
|
This would be really nice to have. |
I find it a little frustrating that when I have a sum-of-products data type, I can't easily access the fields by their name. I do understand the technical limitations here, but I've still been thinking about workarounds. One that would suit my purposes pretty well is to have a traversal to a field name.
Motivation and Explanation
Ideally, if I have a data type such as
and I have a value
Baz 3 True
, then I could do a prism-constructor access ofBaz
followed by a lens-field access onb
. Unfortunately, the prism forBaz
has typePrism' Foo (Int, Bool)
-- Haskell's lack of anonymous records strikes!At a user level, I can make data types for all of these, as in:
Not only is this some annoying type bloat and makes data construction more irritating, but this creates the problem that I no longer have the free lens
field @"a" :: Lens' Foo Int
.I could also define, for instance,
which is a valid, if verbose, option. Template Haskell could help, especially if there are a lot of fields.
As it turns out, there are many cases where one doesn't need the full power of a prism but still wants access to fields that are not defined in every constructor. Obviously, the library maintainer understands this given the traversals in Data.Generics.Product.Types and Data.Generics.Product.Param. I propose this be extended so that one can get a traversal from just a field name. I imagine something like:
This would be very similar to
HasField
except that it would construct a traversal rather than a lens, and it would not check to make sure that the field is present in every constructor. I think, as good form, it should probably make sure that the field is present in at least one constructor, but technically, this wouldn't be necessary.For Your Consideration
The text was updated successfully, but these errors were encountered: