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

Swift 5.7 Improvements #261

Merged
merged 8 commits into from
Dec 8, 2022
Merged

Swift 5.7 Improvements #261

merged 8 commits into from
Dec 8, 2022

Conversation

stephencelis
Copy link
Member

@stephencelis stephencelis commented Dec 8, 2022

While we have some ideas to take Swift 5.7 even further, we need more time to let the details bake. Till then, we can still offer a quality-of-life improvement for folks using Swift 5.7, especially those working on Advent of Code 2022!

Improving Parsing's builder limits

This is perhaps the most notable improvement for now.

Currently, Parsing's builders limits the number of parsers allowed in a block because of how many overloads need to be maintained and generated.

Using Swift 5.7's new buildPartialBlock, we can greatly improve upon this limit:

Builder Block limit swift(<5.7) Block limit swift(>=5.7)
OneOfBuilder 10
ParserBuilder 6 10–∞*

* Up to 10 non-Void captures in a block, plus unlimited Void captures.

This should make working with larger builders much easier. The limit of 10 captures is arbitrary and could be expanded. If you hit it, please let us know!

Adding primary associated types

We've added primary associated types to a number of protocols:

  • Parser<Input, Output>
  • ParserPrinter<Intput, Output>
  • Conversion<Input, Output>
  • PrependableCollection<Element>

This will allow you to express and constrain these protocols in a more lightweight, natural manner.

Formatter parser-printer support

We've added support for using formatters directly in your parser printers with the Formatted parser-printer:

let total = ParsePrint {
  "TOTAL: "
  Formatted(.currency(code: "USD"))
}

try total.parse("TOTAL: $42.42")  // 42.42
try total.print(99.95)            // "TOTAL: $99.95"

Formatted takes any of the many formatters shipping in iOS 15 and more recently.


We have more plans for Parsing in the coming months to take even greater advantage of modern Swift features, and we hope to explore them soon!

While we have some ideas to take Swift 5.7 even further, we need more
time to let the details bake. Till then, we can still offer a
quality-of-life improvement for folks using Swift 5.7, especially those
working on Advent of Code 2022!

Improving Parsing's builder limits
----------------------------------

This is perhaps the most notable improvement for now.

Currently, Parsing's builders limits the number of parsers allowed in a
block because of how many overloads need to be maintained and generated.

Using Swift 5.7's new [`buildPartialBlock`][se-0348], we can greatly
improve upon this limit:

| Builder          | Block limit `swift(<5.7)` | Block limit `swift(>=5.7)` |
| ---------------- | ------------------------- | -------------------------- |
| `OneOfBuilder`   | 10                        | ∞                          |
| `ParserBuilder`  | 6                         | 10–∞*                      |

\* Up to 10 non-`Void` captures in a block, plus unlimited `Void`
captures.

This should make working with larger builders much easier. The limit of
10 captures is arbitrary and could be expanded. If you hit it, please
[let us know][discussions]!

[se-0348]: https://github.com/apple/swift-evolution/blob/main/proposals/0348-buildpartialblock.md
[discussions]: https://github.com/pointfreeco/swift-parsing/discussions

Adding primary associated types
-------------------------------

We've added primary associated types to a number of protocols:

  * `Parser<Input, Output>`
  * `ParserPrinter<Intput, Output>`
  * `Conversion<Input, Output>`
  * `PrependableCollection<Element>`

This will allow you to express and constrain these protocols in a more
lightweight, natural manner.

Formatter parser-printer support
--------------------------------

We've added support for using formatters directly in your parser
printers with the `Formatted` parser-printer:

```swift
let total = ParsePrint {
  "TOTAL: "
  Formatted(.currency(code: "USD"))
}

try total.parse("TOTAL: $42.42")  // 42.42
try total.print(99.95)            // "TOTAL: $99.95"
```

`Formatted` takes any of the many formatters shipping in iOS 15 and more
recently.

---

We have more plans for Parsing in the coming months to take even greater
advantage of modern Swift features, and we hope to explore them soon!
@stephencelis stephencelis force-pushed the swift-5-7-improvements branch from 9ed8959 to d316141 Compare December 8, 2022 15:35
@tgrapperon
Copy link
Contributor

tgrapperon commented Dec 8, 2022

Should the leaking Xcode schemes issue be fixed in this QoL PR, or is this orthogonal?

@stephencelis
Copy link
Member Author

@tgrapperon I didn't realize this package still had that problem, but I'll tackle it in another PR before release!

@stephencelis stephencelis merged commit 645c688 into main Dec 8, 2022
@stephencelis stephencelis deleted the swift-5-7-improvements branch December 8, 2022 18:38
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