Upgrade to Pro — share decks privately, control downloads, hide ads and more …

🀄️ on swiftc

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for giginet giginet PRO
April 14, 2026

🀄️ on swiftc

Avatar for giginet

giginet PRO

April 14, 2026

More Decks by giginet

Other Decks in Technology

Transcript

  1. struct Pair< T1: TileType, T2: TileType >: AnyPair {} extension

    Pair: ValidPair where T1 == T2 {} struct Meld< T1: TileType, T2: TileType, T3: TileType >: AnyMeld {}
  2. struct Tile<Suit: SuitType, let rank: Int>: TileType {} typealias ᤈ<let

    rank: Int> = Tile<ᤈࢠ, rank> typealias ౵<let rank: Int> = Tile<౵ࢠ, rank> typealias ࡧ<let rank: Int> = Tile<ࡧࢠ, rank> ᤈ<1> ࡧ<4> ౵<8> SE-0452 Integer Generic Parameters
  3. #Playground { let identicalMeld = Meld<ᤈ<2>, ᤈ<2>, ᤈ<2>>() identicalMeld is

    ValidMeld } // ✅ true #Playground { let invalidMeld = Meld<ᤈ<2>, ౵<2>, ᤈ<3>>() invalidMeld is ValidMeld } // ❌ false
  4. extension Meld: ValidMeld where Suit1 == Suit2, Suit2 == Suit3,

    rank1 + 1 == rank2, rank2 + 1 == rank3 {}
  5. extension Meld: ValidMeld where Suit1 == Suit2, Suit2 == Suit3,

    rank1 + 1 == rank2, rank2 + 1 == rank3 {}
  6. protocol Rank { associatedtype Next: Rank } struct One: Rank

    { typealias Next = Two } struct Two: Rank { typealias Next = Three } …
  7. protocol Rank { associatedtype Next: Rank } struct `1`: Rank

    { typealias Next = `2` } struct `2`: Rank { typealias Next = `3` } … ᤈ<`1`> SE-0451 Raw Identi fi ers
  8. extension Meld: ValidMeld where T1.S == T2.S, T2.S == T3.S,

    T1.R.Next == T2.R, T2.R.Next == T3.R {}
  9. extension Hand: Win where H: ValidPair, M1: ValidMeld, M2: ValidMeld,

    M3: ValidMeld, M4: ValidMeld {} #Playground { let hand = Hand< Pair<ᤈ<`1`>, ᤈ<`1`>>, Meld<ᤈ<`2`>, ᤈ<`3`>, ᤈ<`4`>>, Meld<౵<`5`>, ౵<`6`>, ౵<`7`>>, Meld<ࡧ<`3`>, ࡧ<`4`>, ࡧ<`5`>>, Meld<ࡧ<`7`>, ࡧ<`8`>, ࡧ<`9`>> >() hand is Win } // ✅ true
  10. /// Identical Meld (ࠁࢠ) extension Meld: ValidMeld where T1 ==

    T2, T2 == T3 {} /// Sequential Meld (ॱࢠ) extension Meld: ValidMeld where T1.S == T2.S, T2.S == T3.S, T1.R.Next == T2.R, T2.R.Next == T3.R {}
  11. /// Identical Meld (ࠁࢠ) extension Meld: ValidMeld where T1 ==

    T2, T2 == T3 {} /// Sequential Meld (ॱࢠ) extension Meld: ValidMeld where T1.S == T2.S, T2.S == T3.S, T1.R.Next == T2.R, T2.R.Next == T3.R {}
  12. ❌ ❌ Union Types Arithmetic computation Can’t express sequential numbers

    with Integer Generics Parameters Can’t conform a protocol both identical and sequential states Pure Swift Type System limitations
  13. #Playground { let result = #validate([ ࡧ<4>(), ࡧ<4>(), ᤈ<2>(), ᤈ<3>(),

    ᤈ<4>(), ᤈ<5>(), ᤈ<6>(), ᤈ<7>(), ౵<3>(), ౵<4>(), ౵<5>(), ࡧ<6>(), ࡧ<7>(), ࡧ<8>() ]) result is Win } // ✅ true
  14. @freestanding(expression) public macro validate(_ tiles: [14 of any Tile]) ->

    any Win = #externalMacro(module: "MacroValidatorMacros", type: "ValidateMacro") SE-0483 InlineArray Type Sugar SE-0382 Expression Macros
  15. let win = #validate([ ࡧ<4>(), ࡧ<4>(), ᤈ<2>(), ᤈ<3>(), ᤈ<4>(), ᤈ<5>(),

    ᤈ<6>(), ᤈ<7>(), ౵<3>(), ౵<4>(), ౵<5>(), ࡧ<6>(), ࡧ<7>(), ࡧ<8>() ]) win is அϠΦ win is ฏ࿨ win is ରʑ࿨ // ✅ true // ✅ true // ❌ false
  16. ✅ ✅ Winning Pattern detection Automatic Head/Meld grouping Macro can

    automatically group 14 tiles into head/melds Macro can detect Winning Patterns (໾) Expression Macros
  17. Conclusion • Pure Swift Type System is powerful, but limited

    • Impossible to express complex game logics • Swift Macro is the Silver Bullet • Can represent the complex logic in the compilation time calculation