[英]Swift Keypath Generic and Subclassing
我正在研究一個超類及其子類具有不同屬性的類,但所有相同的類型在分配之前都需要相同的處理。 我想出了一個非常人為和簡化的例子,說明我試圖對關鍵路徑做些什么,可能使用泛型,包括非最佳但有效的變體。
class OriginalClass {
// this will only allow for properties that exist available on the base `OriginalClass` (and that makes sense)
func updateAProperty(to value: Int, keyPath: ReferenceWritableKeyPath<OriginalClass, Int>) {
// lots of custom, but common logic
self[keyPath: keyPath] = value
}
// this *works*, but I don't like the cast I have to do on the first line, and the call site requires explicit
// keypaths (including the type)
func updateAPropertyTwo<GenClass>(to value: Int, keyPath: ReferenceWritableKeyPath<GenClass, Int>) {
guard let self = self as? GenClass else { return }
self[keyPath: keyPath] = value
}
// ideally, i want to do something like this. Basically, the compiler should (aka i WANT it to) be able to tell that
// im working off a subclass of OriginalClass and provide the keypaths available to the subclass in addition to those
// on the base, superclass.
// func idealNonworking(to value: Int, keyPath: ReferenceWritableKeyPath<*AutomaticallyReplacedWithWhateverSubclass*, Int>) {
// // lots of custom, but common logic
// self[keyPath: keyPath] = value
// }
// this complains that `Same-type requirement makes generic parameter 'GenClass' non-generic`, but afaik, if it DID
// work it SHOULD include the subclass properties (but it doesn't, to be clear)
// func alternativeIdealYetNonworking<GenClass>(to value: Int, keyPath: ReferenceWritableKeyPath<GenClass, Int>) where GenClass == Self {
// // lots of custom, but common logic
// self[keyPath: keyPath] = value
// }
}
class SecondClass: OriginalClass {
var subclassValue = 0
func nonWorkingExample() {
// updateAProperty(to: subclassValue + 1, keyPath: \.subclassValue)
}
func subOptimalWorkingExample() {
updateAPropertyTwo(to: subclassValue + 1, keyPath: \SecondClass.subclassValue)
// updateAPropertyTwo(to: subclassValue + 1, keyPath: \Self.subclassValue) // runs into a runtime demangling error
// updateAPropertyTwo(to: subclassValue + 1, keyPath: \.subclassValue)
}
// func optimalYetNonworkingExample() {
// idealNonworking(to subclassValue + 1, keyPath: \.subclassValue)
// }
}
let test = SecondClass()
print(test.subclassValue)
test.subOptimalWorkingExample()
print(test.subclassValue)
現在,我知道為什么第一個不起作用(關鍵路徑類型由OriginalClass
可用屬性定義),但我不確定為什么最后一個不起作用。 當然,這比是否有人知道如何做到這一點更重要
Self
在協議擴展中是允許的,所以我只寫了:
protocol P {
// put whatever methods and properties from OriginalClass the "lots of custom,
// but common logic" need here...
}
class OriginalClass : P {}
extension P {
func updateAProperty(to value: Int, keyPath: ReferenceWritableKeyPath<Self, Int>) {
// lots of custom, but common logic
self[keyPath: keyPath] = value
}
}
和用法:
class SecondClass: OriginalClass {
var subclassValue = 0
func workingExample() {
updateAProperty(to: subclassValue + 1, keyPath: \.subclassValue)
}
}
let test = SecondClass()
print(test.subclassValue)
test.workingExample()
print(test.subclassValue)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.