makeFields without lenses

Good morning to everyone. I really love what makeFields from Control.Lens.TH does: generating a typeclasse HasXXX for each field _typeXXX and instances of the class for each type that has a record field with the same name. However, it generates lenses, which at this time I don’t use in my little project. I'll learn to use them well sooner or later, I promise! In the meantime, is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses? Or can I configure the parameters of makeFieldsWith to generate simple functions instead of lenses? Another related question: if I use makeFields in more than one module, and different records have fields with the same name, the generated typeclasses will have the same name but will be different, because declared twice in different modules, am I right? Is there a way to use a unique typeclass in the whole project for every generated field of the same name? Thank you very much, Nicola

The compiler automatically provides accessor functions already. You can't have more than one function with the same name in the same module without a qualified import.
Ben
On Sat, Oct 4, 2014 at 2:57 PM, Nicola Gigante
Good morning to everyone. I really love what makeFields from Control.Lens.TH does: generating a typeclasse HasXXX for each field _typeXXX and instances of the class for each type that has a record field with the same name. However, it generates lenses, which at this time I don’t use in my little project. I'll learn to use them well sooner or later, I promise! In the meantime, is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses? Or can I configure the parameters of makeFieldsWith to generate simple functions instead of lenses? Another related question: if I use makeFields in more than one module, and different records have fields with the same name, the generated typeclasses will have the same name but will be different, because declared twice in different modules, am I right? Is there a way to use a unique typeclass in the whole project for every generated field of the same name? Thank you very much, Nicola _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Il giorno 04/ott/2014, alle ore 13:03, Benjamin Edwards
The compiler automatically provides accessor functions already. You can't have more than one function with the same name in the same module without a qualified import.
Yes, that’s exactly the problem solved by makeFields. You declare the fields with a different name for each type but you get a lens with the same name for each type by declaring the suitable type class and instances. What I was asking is if is it possible to do the same generating simple functions instead of lenses. The functions generated by the compiler with the record syntax are not what I want, because the name must be unique for any single type.
Ben
Thanks, Nicola

On Sat, Oct 4, 2014 at 5:57 AM, Nicola Gigante
However, it generates lenses, which at this time I don’t use in my little project. I'll learn to use them well sooner or later, I promise! In the meantime, is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses?
Anything that does this creates lenses. That does not mean that they all create Control.Lens, though; a lens is a general abstraction, not a trademark. The simplest version, focused solely on this use case, is probably the fclabels package. Note that the reason they do what you need is that they are lenses. Simple accessors will conflict as you described. fclabels is still useful if you're only interested in lenses for record field accessors, though, since the full lens package is rather heavyweight in that case --- and as a bonus, you'll be getting a head start on the more general concept, without getting dropped into the deep end of the pool. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Nicola,
Could you be looking for the OverloadedRecordFields[0] extension? This is
an extension that I *think* is slated to be in 7.10, and might do what you
want (though won't help you now).
If you declare two data types with the same field name (e.g. data A = A {
hello :: Int} and data B = B { hello :: Int }), the extension will generate
some typeclasses and type families so that you can use `hello` on both data
types.
(I haven't used this myself, but have been looking forward to it, so if
anyone wants to correct me on anything I've said please do so.)
[0] https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields
On Sat, Oct 4, 2014 at 6:53 AM, Brandon Allbery
On Sat, Oct 4, 2014 at 5:57 AM, Nicola Gigante
wrote: However, it generates lenses, which at this time I don’t use in my little project. I'll learn to use them well sooner or later, I promise! In the meantime, is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses?
Anything that does this creates lenses. That does not mean that they all create Control.Lens, though; a lens is a general abstraction, not a trademark. The simplest version, focused solely on this use case, is probably the fclabels package.
Note that the reason they do what you need is that they are lenses. Simple accessors will conflict as you described. fclabels is still useful if you're only interested in lenses for record field accessors, though, since the full lens package is rather heavyweight in that case --- and as a bonus, you'll be getting a head start on the more general concept, without getting dropped into the deep end of the pool.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses? Or can I configure the parameters of makeFieldsWith to generate simple functions instead of
lenses? There are none that I am aware of. I certainly don't think Control.Lens supports this. Another related question: if I use makeFields
in more than one module, and different records have fields with the same name, the generated typeclasses will have the same name but will be different, because declared twice in different modules, am I right?
I believe this is correct. Is there a way to use a unique
typeclass in the whole project for every generated field of the same name?
If the typeclass already exists in the module, then it won't be generated. So if you are working on module B, and you import module A which exports the class in question, then I believe module B will just use the class from module A instead of creating its own. There are various helper functions in Control.Lens.TH, such as "makeClassyFor", which are meant to help with this. However, I don't think your use case is covered well by these helper functions. If you get in touch with the lens devs (file an issue in their github tracker, or chat with them on irc), I'm sure they can help you accomplish what you desire. n.b. lenses aren't all that scary. It is quite simple to retrieve the "simple accessor" from a lens. Just use "view". import Control.Lens.Getter (view) -- given this typeclass operation fooLens :: HasFoo a => Lens' a Foo -- just use view on it to extract the getter portion of the lens foo :: HasFoo a => a -> Foo foo = view fooLens -- Dan Burton On Sat, Oct 4, 2014 at 11:02 AM, Andrew Gibiansky < andrew.gibiansky@gmail.com> wrote:
Nicola,
Could you be looking for the OverloadedRecordFields[0] extension? This is an extension that I *think* is slated to be in 7.10, and might do what you want (though won't help you now).
If you declare two data types with the same field name (e.g. data A = A { hello :: Int} and data B = B { hello :: Int }), the extension will generate some typeclasses and type families so that you can use `hello` on both data types.
(I haven't used this myself, but have been looking forward to it, so if anyone wants to correct me on anything I've said please do so.)
[0] https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields
On Sat, Oct 4, 2014 at 6:53 AM, Brandon Allbery
wrote: On Sat, Oct 4, 2014 at 5:57 AM, Nicola Gigante
wrote: However, it generates lenses, which at this time I don’t use in my little project. I'll learn to use them well sooner or later, I promise! In the meantime, is there some module out there than provides a template haskell function that do the same thing but generating simple accessor functions instead of lenses?
Anything that does this creates lenses. That does not mean that they all create Control.Lens, though; a lens is a general abstraction, not a trademark. The simplest version, focused solely on this use case, is probably the fclabels package.
Note that the reason they do what you need is that they are lenses. Simple accessors will conflict as you described. fclabels is still useful if you're only interested in lenses for record field accessors, though, since the full lens package is rather heavyweight in that case --- and as a bonus, you'll be getting a head start on the more general concept, without getting dropped into the deep end of the pool.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Il giorno 04/ott/2014, alle ore 20:02, Andrew Gibiansky
Nicola,
Could you be looking for the OverloadedRecordFields[0] extension? This is an extension that I think is slated to be in 7.10, and might do what you want (though won't help you now).
If you declare two data types with the same field name (e.g. data A = A { hello :: Int} and data B = B { hello :: Int }), the extension will generate some typeclasses and type families so that you can use `hello` on both data types.
(I haven't used this myself, but have been looking forward to it, so if anyone wants to correct me on anything I've said please do so.)
[0] https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields
Yes, that extension would be exactly what I want. When GHC 7.10 comes out I’ll use it for sure! In the meantime I hoped there was a package to overcome this but as Dan has said, it seems it doesn’t exist. Thank you everybody for all the answers. Greetings, Nicola
participants (5)
-
Andrew Gibiansky
-
Benjamin Edwards
-
Brandon Allbery
-
Dan Burton
-
Nicola Gigante