https://github.com/EricRabil/EnumMagic
Dynamically initialize enums in Swift
https://github.com/EricRabil/EnumMagic
Last synced: 11 months ago
JSON representation
Dynamically initialize enums in Swift
- Host: GitHub
- URL: https://github.com/EricRabil/EnumMagic
- Owner: EricRabil
- License: mit
- Created: 2021-08-30T00:36:42.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-08-30T01:13:52.000Z (almost 5 years ago)
- Last Synced: 2024-11-18T18:48:01.942Z (over 1 year ago)
- Language: Swift
- Size: 9.77 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EnumMagic
Dynamically construct an enum value based on its case name and an array of associated values
## Beware dragons!
This library brazenly works with unsafe bytes and bitcasts it back into the Swift type system. It is compatible with the current [Fragile Enum Layout](https://github.com/apple/swift/blob/2950c38a5d4f1f028d3786a278ae61eaf501f0d0/docs/ABI/TypeLayout.rst) specification.
## How to use it
```swift
import EnumMagic
enum IntDoubleOrBignum { // => LLVM <{ i64, i2 }>
case Int(Int) // => <{ i64, i2 }> { %Int, 0 }
case Double(Double) // => <{ i64, i2 }> { (bitcast %Double to i64), 1 }
case Bignum(Bignum) // => <{ i64, i2 }> { (ptrtoint %Bignum to i64), 2 }
case Alexis(Int, Double)
case Blank1 // => <{ i64, i2 }> { 0, 3 }
case Blank2 // => <{ i64, i2 }> { 1, 3 }
case ASDFA(Int, Bignum)
}
let dynamicInt = CreateEnum(IntDoubleOrBignum.self, "Int", payload: [5])
let dynamicBlank1 = CreateEnum(IntDoubleOrBignum.self, "Blank1")
let dynamicBlank2 = CreateEnum(IntDoubleOrBignum.self, "Blank2")
let dynamicASDFA = CreateEnum(IntDoubleOrBignum.self, "ASDFA", payload: [6, Bignum(num: 5)])
```