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

Simplify Inject/Interpret for 1-field records #1315

Merged
merged 5 commits into from Sep 15, 2019
Merged

Conversation

Gabriella439
Copy link
Collaborator

Fixes #346

Now a Haskell type like:

data Example = Foo Bool | Bar Text

... corresponds to this Dhall type:

{ Foo : Bool | Bar : Text }

Fixes #346

Now a Haskell type like:

```haskell
data Example = Foo Bool | Bar Text
```

... corresponds to this Dhall type:

```dhall
{ Foo : Bool | Bar : Text }
```
Copy link
Collaborator

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

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

Cheers! This looks like a very nice improvement! 👍

@@ -161,56 +161,62 @@ data NonEmptyUnion = N0 Bool | N1 Natural | N2 Text
data Enum = E0 | E1 | E2
deriving (Eq, Generic, Inject, Interpret, Show)

data Mixed = M0 Bool | M1 | M2 ()
data Mixed = M0 Bool | M1 | M2 () | M3 Bool () | M4 { x :: Double, y :: Double }
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess there are still more interesting cases like

| M5 { x :: Text }

and

| M6 { x :: () }

Handling () fields consistently seems a bit tricky, but I don't think it's particularly important.

* 0, 1, or 2 fields
* Fields of type `()` or `Double`
* Named or anonymous fields
@mergify mergify bot merged commit ca86ed6 into master Sep 15, 2019
@mergify mergify bot deleted the gabriel/one_field branch September 15, 2019 03:38
@Profpatsch
Copy link
Member

Ah great, I was very surprised by the implicit _1, _2, … fields.

@patrickmn
Copy link
Sponsor Collaborator

I think this broke a place where I'm deserializing a one-field record:

data MyRecord = MyRecord { foo :: Text }
myRecord = { foo : Text }

-->

Error: Expression doesn't match annotation

{ myRecord : ?
              - Text
              + { ? : ? }
...

Just making sure that's intentional.

@Gabriella439
Copy link
Collaborator Author

@patrickmn: It was intentional, but we can make this behavior configurable via InterpretOptions

@patrickmn
Copy link
Sponsor Collaborator

I understand it now, the error was just a bit puzzling until I looked at the recent merges.

My data type was just a placeholder, so it's no bother :)

Gabriella439 added a commit that referenced this pull request Sep 17, 2019
This builds on top of #1315 to minimize disruption by disabling the
breaking change by default and instead requiring the user to opt in
by setting a new `collapseSingletonRecords` option to `True`.

The additional tests added to verify this also caught a bug in the
`Interpret` instance for functions, which this change also fixes.
@Gabriella439
Copy link
Collaborator Author

@patrickmn: I decided to disable the breaking change by default, at least for the upcoming release, by guarding it behind an option. See: #1321

mergify bot pushed a commit that referenced this pull request Sep 19, 2019
* Disable 1-field simplification by default

This builds on top of #1315 to minimize disruption by disabling the
breaking change by default and instead requiring the user to opt in
by setting a new `collapseSingletonRecords` option to `True`.

The additional tests added to verify this also caught a bug in the
`Interpret` instance for functions, which this change also fixes.

* Change to three-valued option

... based on feedback from @sjakobi

This change the option to a three-valued option:

* `Bare`    - 1-field constructor does not include a nested record
* `Wrapped` - 1-field constructor always includes a nested record
* `Smart`   - Named fields that don't begin with `_` include a nested record

The default is `Wrapped` (for backwards compatibility), but users will
probably want to eventually switch to `Smart`

* Don't depend on `fieldModifier` for determining if a field is anonymous

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

Successfully merging this pull request may close these issues.

Sum types produce empty backtick quoting
4 participants