SkillAgentSearch skills...

EnumMagic

Dynamically initialize enums in Swift

Install / Use

/learn @EricRabil/EnumMagic
About this skill

Quality Score

0/100

Supported Platforms

Universal

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 specification.

How to use it

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)])

Related Skills

View on GitHub
GitHub Stars14
CategoryDevelopment
Updated3y ago
Forks0

Languages

Swift

Security Score

75/100

Audited on Feb 27, 2023

No findings