Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sema/SILGen/IRGen] Implement method & initializer keypaths. #78823

Merged
merged 29 commits into from
Mar 20, 2025

Conversation

amritpan
Copy link
Member

@amritpan amritpan commented Jan 22, 2025

This is the implementation for Method and Initializer Keypaths which extends keypath usage to include references to methods and initializers:

struct S {
  static let millenium = 3
  var year = 2024
  init() {}
  init(val value: Int = 2024) { year = value }
  
  func add(this: Int) -> Int { this + this}
  func add(that: Int) -> Int { that + that }
  static func subtract(_ val: Int) -> Int { return millenium - val }
  nonisolated func nonisolatedNextYear() -> Int { return year + 1 }
  consuming func consume() { print(year) }

  subscript(index: Int) -> Int { return year + index }
}

let kp1: KeyPath<S, () -> S> = \S.Type.init
let kp2: KeyPath<S, S> = \S.Type.init()
let kp3: KeyPath<S, (Int) -> S> = \S.Type.init(val:)
let kp4: KeyPath<S, S> = \S.Type.init(val: 2025)
let kp5: KeyPath<S, (Int) -> Int> = \S.add(this:)
let kp6: KeyPath<S, Int> = \S.add(that: 1)
let kp7: KeyPath<S, Int> = \S.Type.subtract(1)
let kp8: KeyPath<S, Int> = \S.nonisolatedNextYear()
let kp9: KeyPath<S, Int> = \S.nonisolatedNextYear().signum()
let kp10: KeyPath<S, String> = \S.nonisolatedNextYear().description
let kp11: KeyPath<S, ()> = \S.consume()
let kp12: KeyPath<S, Int> = \S.Type.init()[1]

inout, mutating, throws, and async methods are not allowed for existing keypath component types and similarly not supported by this implementation. escaping is also unsupported as keypath method arguments must be Hashable.

This implementation is accompanied by changes in swift-syntax and foundation.

@amritpan amritpan force-pushed the method-keypaths branch 5 times, most recently from e993fb9 to 9e2ce44 Compare March 5, 2025 13:49
@amritpan amritpan force-pushed the method-keypaths branch 3 times, most recently from 6e5f20a to 3843b78 Compare March 6, 2025 15:57
@amritpan
Copy link
Member Author

amritpan commented Mar 6, 2025

swiftlang/swift-syntax#2950

@swift-ci Please Test Source Compatibility.

@amritpan
Copy link
Member Author

@swift-ci Please test.

@amritpan amritpan enabled auto-merge March 19, 2025 15:57
@amritpan
Copy link
Member Author

@swift-ci Please test.

@amritpan amritpan merged commit 3c30d68 into swiftlang:main Mar 20, 2025
5 checks passed
@amritpan amritpan deleted the method-keypaths branch March 20, 2025 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants