Skip to content

beccadax/Upconvert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Upconvert: conveniently perform safe conversions

The Upconvert framework allows convenient, lossless conversions between different types in Swift 4. Conversion is performed using the upconversion operator, ^:

let small: Int8 = 1
let large: Int = 100_000
print(large + ^small)       // => 100002

Upconversion is only allowed when the destination type is large enough to contain all possible values of the source type. Otherwise, your code will not compile.

let small: Int8 = 1
let large: Int = 100_000

large + ^small      // OK
^large + small      // Error

Supported upconversions

Upconvert ships with the following conversions built in:

  • Int8Int16Int32IntInt64
  • UInt8UInt16UInt32UIntUInt64
  • FloatCGFloatDoubleFloat80
  • SubstringString
  • UnicodeScalarCharacterString
  • KeyPath → getter function

The KeyPath upconversion allows you to use key paths with functions like map and filter:

let names = people.map(^\.name)

You can add your own upconversions by conforming types to the Upconvertible protocol.

Installation

Just clone it and put the project into Xcode. Pull requests to support package managers are welcome.

Limitations

  • Downconversions—potentially lossy conversions—are not supported.

  • Each type can only upconvert to one other type (plus the type it upconverts to, recursively). This is why you can't upconvert a UInt32 to an Int64.

  • The ^ operator will only recurse so far (five levels currently).

  • The Int and UInt conversion sequences don't support the new DoubleWidth type due to limitations in its current implementation.

  • This design is bleeding-edge; don't consider the interface to be stable yet.

Author

Brent Royal-Gordon, Architechies.

Copyright

Copyright © 2017 Architechies. Distributed under the MIT License.

About

Conveniently perform safe type conversions in Swift 4.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages