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

updated documentation strings to include empty tag in a union #2625

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

amkhlv
Copy link

@amkhlv amkhlv commented Jan 4, 2025

The existing documentation on union decoder misses the case when one of constructors is an empty tag. This patch adds an example of that.

Copy link
Collaborator

@Gabriella439 Gabriella439 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what you want

Comment on lines 971 to 974
adapt (Queued n) = Just (Left n)
adapt (Result t) = Just (Right (Left t))
adapt (Errored e) = Just (Right (Right e))
adapt Unreachable = Nothing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
adapt (Queued n) = Just (Left n)
adapt (Result t) = Just (Right (Left t))
adapt (Errored e) = Just (Right (Right e))
adapt Unreachable = Nothing
adapt (Queued n) = Left n
adapt (Result t) = Right (Left t)
adapt (Errored e) = Right (Right (Left e))
adapt Unreachable = Right (Right (Right ()))

Comment on lines 988 to 991
adapt (Queued n) = Just (Left n)
adapt (Result t) = Just (Right (Left t))
adapt (Errored e) = Just (Right (Right e))
adapt Unreachable = Nothing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
adapt (Queued n) = Just (Left n)
adapt (Result t) = Just (Right (Left t))
adapt (Errored e) = Just (Right (Right e))
adapt Unreachable = Nothing
adapt (Queued n) = Left n
adapt (Result t) = Right (Left t)
adapt (Errored e) = Right (Right (Left e))
adapt Unreachable = Right (Right (Right ()))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am getting a bit lost. The documentation on Hackage --- I think it is different from the docstring in the source file which I have been editing. Is this a newer version? What does the adapt function do?
That's why I wanted to compile it, to see the up-to-date documentation. But I could not compile it...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's been a while since the last release to Hackage, which is the reason for the difference. However, I'm working on cutting a new release soon

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stackage would also be great. It would enable Hoogle...

@@ -943,6 +943,7 @@ encodeFieldWith name encodeType = RecordEncoder $ Dhall.Map.singleton name encod
data Status = Queued Natural
| Result Text
| Errored Text
| Unreachable ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| Unreachable ()
| Unreachable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how I had it:

data DateFinished = Current () | Done DATE.Day | Dropped () deriving (Generic, Show, Eq)

and then:

dateFinishedDecoder :: Decoder DateFinished
dateFinishedDecoder =
  union
    ( (Current <$> constructor "Current" unit)
        <> (Done <$> constructor "Done" day)
        <> (Dropped <$> constructor "Dropped" unit)
    )

But if I do as you suggested, i.e. data DateFinished = Current | Done DATE.Day | Dropped deriving (Generic, Show, Eq) --- then it does not compile

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you change that to:

dateFinishedDecoder :: Decoder DateFinished
dateFinishedDecoder =
  union
    ( (Current <$ constructor "Current" unit)  -- Carefully note the `<$`
        <> (Done <$> constructor "Done" day)
        <> (Dropped <$> constructor "Dropped" unit)
    )

… then it should compile

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I got it ! Cute (although smells of witchcraft...). Then, maybe, instead of my suggested patch, would you rather write something pedagogical, in that docstring?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just requested the pull for the correcting patch, as you suggested.

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

Successfully merging this pull request may close these issues.

3 participants