[Haskell-cafe] makeFields without lenses

Dan Burton danburton.email at gmail.com
Sat Oct 4 19:53:17 UTC 2014


>
> 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 at 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 <allbery.b at gmail.com>
> wrote:
>
>> On Sat, Oct 4, 2014 at 5:57 AM, Nicola Gigante <nicola.gigante at gmail.com>
>> 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 at gmail.com
>> ballbery at sinenomine.net
>> unix, openafs, kerberos, infrastructure, xmonad
>> http://sinenomine.net
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141004/821be358/attachment.html>


More information about the Haskell-Cafe mailing list