kareman / Moderator

A simple, modular command line argument parser in Swift.
MIT License
21 stars 2 forks source link

Supporting Both Swift 3 and Swift 4 #4

Closed Kaiede closed 6 years ago

Kaiede commented 6 years ago

I’m using Swift on a Raspberry Pi for aquarium automation. The issue is that we are stuck with Swift 3.1.1 for the time being, but still want to leverage libraries like this one. And this one was the easiest to bootstrap back into Swift 3 for my purposes.

I’ve put together a set of changes in my fork that add extensions to String for the Swift 4-only APIs you call, based on the original Swift 3 code you had. I also changed the Travis yml file so that CI builds check both 3.1.1 and 4.1.2. While I wouldn’t expect folks to test against both versions of Swift, and they could accidentally use a Swift 4 API without thinking of compatibility, the CI should help catch those sorts of issues.

The one change I had to make that I’m not entirely happy with has to do with the empty tuple you use during a map to save space. The code doesn’t care about the result, so it makes sense, but it only really works in Swift 4. Swift 3 complains about using an empty tuple in this way. I made it work by using a Bool instead, but perhaps there’s a better approach here that I’m missing.

I can send the Pull Request along as it has been working well for my project, and Travis is happy, except that flatMap apparently just got deprecated in Swift 4.1 (just a warning for now). Go figure.

kareman commented 6 years ago

Nice, I could add those changes to the Swift3 branch. I’d love a pull request.

FlatMap hasn’t been deprecated, but the version that takes a sequence of optionals and returns an array of non-optionals has been renamed to compactMap.

kareman commented 6 years ago

5 has been merged