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

"奇妙"なSwift

Avatar for kntk kntk
September 21, 2025
190

 "奇妙"なSwift

iOSDC2025 day2 LT

Avatar for kntk

kntk

September 21, 2025
Tweet

Transcript

  1. let range: Range<Int> = 0..<1 // ..<͸RangeΛੜ੒͢Δԋࢉࢠ extension Comparable {

    public static func ..< (minimum: Self, maximum: Self) -> Range<Self> {} } ྫ3BOHFͱTVCTDSJQU
  2. ྫ3BOHFͱTVCTDSJQU let range: Range<Int> = 0..<1 print([1, 2][range]) // ArraySlice<Int>([1])

    // ..<͸RangeΛੜ੒͢Δԋࢉࢠ extension Comparable { public static func ..< (minimum: Self, maximum: Self) -> Range<Self> {} }
  3. ྫ6OCPVOEFE3BOHFͩͱʜ let range: UnboundedRange<Int> = ... print([1, 2][range]) // ArraySlice<Int>([1,

    2]) extension Comparable { public static func ... () -> UnboundedRange<Self> {} }
  4. ྫ6OCPVOEFE3BOHFͩͱʜ let range: UnboundedRange<Int> = ... print([1, 2][range]) // ArraySlice<Int>([1,

    2]) extension Comparable { public static func ... () -> UnboundedRange<Self> {} 🔴 }
  5. ྫ6OCPVOEFE3BOHF public typealias UnboundedRange = (UnboundedRange_)->() public enum UnboundedRange_ {

    public static postfix func ... (_: UnboundedRange_) -> () { } } // ʮ...ʯ͸ؔ਺ͷࢀরͱͯ͠هड़ [1, 2][...]
  6. ྫ6OCPVOEFE3BOHF public typealias UnboundedRange = (UnboundedRange_)->() public enum UnboundedRange_ {

    public static postfix func ... (_: UnboundedRange_) -> () { } } // ʮ...ʯ͸ؔ਺ͷࢀরͱͯ͠هड़ [1, 2][...] extension Collection { public subscript(x: UnboundedRange) -> SubSequence {} }
  7. ྫ6OCPVOEFE3BOHF public typealias UnboundedRange = (UnboundedRange_)->() public enum UnboundedRange_ {

    public static postfix func ... (_: UnboundedRange_) -> () { } } // Ϋϩʔδϟʔ΋Ҿ਺ʹೖͬͯ͠·͏ʂ [1, 2][{ _ in }] extension Collection { public subscript(x: UnboundedRange) -> SubSequence {} }
  8. ྫԠ༻ typealias LastIndex = (LastIndex_) -> Void postfix operator ~

    enum LastIndex_ { static postfix func ~(_ l: LastIndex_) -> Void {} } extension MutableCollection where Self: BidirectionalCollection { // shorthand of index(before: endIndex) subscript(_ l: LastIndex) -> Element { get { self[index(before: endIndex)] } set { self[index(before: endIndex)] = newValue } } }
  9. ྫԠ༻ typealias LastIndex = (LastIndex_) -> Void postfix operator ~

    enum LastIndex_ { static postfix func ~(_ l: LastIndex_) -> Void {} } extension MutableCollection where Self: BidirectionalCollection { // shorthand of index(before: endIndex) subscript(_ l: LastIndex) -> Element { get { self[index(before: endIndex)] } set { self[index(before: endIndex)] = newValue } } } var array = [1, 2, 3] array[~] = 10
  10. ྫ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    print("multiple of 3") case { $0.isMultiple(of: 5) }: print("multiple of 5") default: print("other") } func ~=<T>(evaluate: (T) -> Bool, rhs: T) -> Bool { evaluate(rhs) }
  11. ྫ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    print("multiple of 3") case { $0.isMultiple(of: 5) }: print("multiple of 5") default: print("other") } func ~=<T>(evaluate: (T) -> Bool, rhs: T) -> Bool { evaluate(rhs) }
  12. ྫ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    print("multiple of 3") case { $0.isMultiple(of: 5) }: print("multiple of 5") default: print("other") } func ~=<T>(evaluate: (T) -> Bool, rhs: T) -> Bool { evaluate(rhs) }
  13. ྫύλʔϯϚονϯάԋࢉࢠ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    } switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }: }
  14. ྫύλʔϯϚονϯάԋࢉࢠ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    } { $0.isMultiple(of: 3) } ~= Int.random(in: 1..<100) ίϯύΠϥ಺෦ͰಡΈସ͑ ˞FOVNͷύλʔϯϚον͸ผ
  15. ྫύλʔϯϚονϯάԋࢉࢠ switch Int.random(in: 1..<100) { case { $0.isMultiple(of: 3) }:

    } { $0.isMultiple(of: 3) } ~= Int.random(in: 1..<100) func ~=<T>(evaluate: (T) -> Bool, rhs: T) -> Bool { evaluate(rhs) } ίϯύΠϥ಺෦ͰಡΈସ͑
  16. ྫԠ༻ extension Set { static func ~=(lhs: Set<Element>, rhs: Element)

    -> Bool { lhs.contains(rhs) } } let vegitablesIDs: Set<Int> = [1, 2, 3] let fruitsIDs: Set<Int> = [4, 5, 6] func printPlantCategory(plantID: Int) { switch plantID { case vegitablesIDs: print("vegitables") case fruitsIDs: print("fruits") default: print("not vegitables nor fruits") } }