Skip to content

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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions dhall/src/Dhall/Marshal/Encode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

:}

And assume that we have the following Dhall union that we would like to
Expand All @@ -951,6 +952,7 @@ data Status = Queued Natural
> < Result : Text
> | Queued : Natural
> | Errored : Text
> | Unreachable
> >.Result "Finish successfully"

Our encoder has type 'Encoder' @Status@, but we can't build that out of any
Expand All @@ -963,11 +965,13 @@ injectStatus = adapt >$< unionEncoder
( encodeConstructorWith "Queued" inject
>|< encodeConstructorWith "Result" inject
>|< encodeConstructorWith "Errored" inject
>|< encodeConstructorWith "Unreachable" inject
)
where
adapt (Queued n) = Left n
adapt (Result t) = Right (Left t)
adapt (Errored e) = Right (Right e)
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 ()))

:}

Or, since we are simply using the `ToDhall` instance to inject each branch, we could write
Expand All @@ -978,11 +982,13 @@ injectStatus = adapt >$< unionEncoder
( encodeConstructor "Queued"
>|< encodeConstructor "Result"
>|< encodeConstructor "Errored"
>|< encodeConstructor "Unreachable"
)
where
adapt (Queued n) = Left n
adapt (Result t) = Right (Left t)
adapt (Errored e) = Right (Right e)
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...

:}

-}
Expand Down