Skip to content
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

Lenses created by makeLenses should inherit Haddock documentation #614

Open
edsko opened this issue Nov 11, 2015 · 11 comments
Open

Lenses created by makeLenses should inherit Haddock documentation #614

edsko opened this issue Nov 11, 2015 · 11 comments

Comments

@edsko
Copy link

edsko commented Nov 11, 2015

Currently the created lenses don't have any documentation, so if we only export the lens accessors for a datatype and hide the constructors, we lose all documentation about the datatype.

I don't know if this is possible with current Haddock, however.

@ekmett
Copy link
Owner

ekmett commented Nov 11, 2015

Can we actually generate haddocks via template-haskell yet? Back when the code for this was written we couldn't.

@drquicksilver
Copy link

TH can't generate haddock comments as far as I know - here is the GHC trac - https://ghc.haskell.org/trac/ghc/ticket/5467

@glguy
Copy link
Collaborator

glguy commented Nov 11, 2015

If you want to document your lens you can disable type signature generation, write type signatures, document them.

Not only can TH not generate haddock comments, it can't read them.

@ekmett
Copy link
Owner

ekmett commented Nov 11, 2015

In the 11079 ticket I commented about the lack of ability to read as well. Maybe I should move that bit into the 5467 ticket.

@noinia
Copy link

noinia commented May 3, 2022

The GHC ticket (5467) mentioned earlier is now apparently closed due to this commit. I'm haven't really implemented anything using TH before, so does that mean it would now be possible to implement this feature in lens?

@ekmett
Copy link
Owner

ekmett commented May 4, 2022

It seems it would! It would need a bleeding edge GHC obviously, and we might need to review how we use @glguy's template-haskell shim.

@ekmett
Copy link
Owner

ekmett commented May 4, 2022

Seven years from request to "yeah, we could probably do that now" is not our fastest turn around time yet, I admit.

@RyanGlScott
Copy link
Collaborator

Just to be clear, the commit linked above lets you attach Haddock comments to TH-generated declarations, but it does not (AFAICT) let you slurp Haddocks comments from TH quotes. That is to say, GHC won't preserve any Haddock comments one writes in a declareLenses [d| ... |] splice, but it would let you generate fresh Haddock comments of your own choosing.

@ekmett
Copy link
Owner

ekmett commented May 4, 2022

I was hoping that the new getDoc machinery would let us look at the haddocks field by field in the original data constructors and replicate them on the lenses we generated associated with each field. I hadn't considered declareLenses as I er.. never actually use that form.

@ekmett
Copy link
Owner

ekmett commented May 4, 2022

The comment:

getDocs :: GhcMonad m
        => Name
        -> m (Either GetDocsFailure (Maybe HsDocString, IntMap HsDocString))
           -- TODO: What about docs for constructors etc.?

gives me some pause about the viability of that option.

@RyanGlScott
Copy link
Collaborator

RyanGlScott commented May 4, 2022

I hadn't considered declareLenses as I er.. never actually use that form.

OK, good to know. In that case, the template-haskell API available in GHC 9.2 should be sufficient. It's capable of looking up the Haddocks for any declaration, including data constructors:

{-# LANGUAGE TemplateHaskell #-}
module Bug where

import Data.Foldable
import Language.Haskell.TH

-- | Haddocks for 'Foo'.
data Foo =
  -- | Haddocks for 'MkFoo'.
  MkFoo
    { foo1 :: Int
      -- ^ Haddocks for 'foo1'.
    , foo2 :: Bool
      -- ^ Haddocks for 'foo2'.
    }

$(pure [])

$(do let getAndPrint n = getDoc (DeclDoc n) >>= runIO . print
     traverse_ getAndPrint [''Foo, 'MkFoo, 'foo1, 'foo2]
     pure [])

main :: IO ()
main = pure ()
$ runghc-9.2 -haddock Bug.hs
Just " Haddocks for 'Foo'."
Just " Haddocks for 'MkFoo'."
Just " Haddocks for 'foo1'."
Just " Haddocks for 'foo2'."

(I'm not sure what to make of the -- TODO: What about docs for constructors etc.? comment, but it doesn't quite seem accurate.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants