A solution which respects the current language and requirement constraints would look something like this:
class Thing {
public func theAnswer() -> Int {
if let replaceableAnswer = Thing.replaceableAnswer {
return replaceableAnswer(self, self.theAnswerOriginal)
}
else
{
return theAnswerOriginal()
}
}
private func theAnswerOriginal() -> Int {
return 20
}
static var replaceableAnswer: ((_ thing: Thing, _ original: () -> Int) -> Int)?
}
let thing = Thing()
print(thing.theAnswer())
Thing.replaceableAnswer = {
(thing, original) in
return original() + 22
}
print(thing.theAnswer())
I think this shows just how Allow dynamic keyword on non-objc properties and this pitch is different.
It appears that dynamic
has alight been slightly repurposed in Swift by the creation of @dynamicMemberLookup
. Maybe replaceable
would be a more accurate keyword for this.