I recently came across this incorrect code in a project:
struct Video {
var progress: Float? {
willSet {
guard let value = newValue else {
self.progress = nil
return
}
self.progress = min(max(value, 0.0), 1.0)
}
}
}
The intent was to have progress
clamped between 0 and 1 but this code clearly doesn't do that.
These specifics aren't important here but what I find curious is that it wasn't noticed – and I believe that's in part because Xcode gave no warning here. However, if I remove the two uses of explicit self
I do get this warning:
Attempting to store to property 'progress' within its own willSet, which is about to be overwritten by the new value
I suspect that the author of that flawed code above would've immediately corrected it had this warning been shown to them.
My question:
Is there any good reason why the use of explicit self
should affect whether or not this warning gets shown in this case? Personally I don't see why the use of explicit self
here would obfuscate the problem that the warning points out.