Swift: Recursively calling a getter/setter should be an error

Originator:brent
Number:rdar://17284560 Date Originated:2014-06-12
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 6.0 beta 1
Classification:UI/Usability Reproducible:Always
 
Summary:
Calling a computed property's getter or setter from within itself, without using super, almost always causes infinite recursion. It should be an error.

Steps to Reproduce:
1. Create a Swift playground.
2. Declare a computed property like so:

class Foo {
    class var foo: Int { return self.foo }
}

3. Try to use Foo.foo in any way.

Expected Results:
The code in step 2 is flagged as an error and will not compile.

Actual Results:
The code compiles and causes infinite recursion. In a playground, this apparently manifests as the playground refusing to ever evaluate any new code.

Additional Notes:
When "self" is left off, this produces a warning, but I see no reason why this should be a mere warning, nor why "self" should suppress it. Getters in particular have no parameters you can use to eventually terminate recursion, so the only way the recursion could possibly terminate would be if they had side effects—a pattern which should probably be discouraged, to say the least. Setters, at least, could vary the value to eventually terminate recursion, but this still seems esoteric.

This might be harmless, except that many developers don't realize that computed properties have no storage and naïvely try access it with a recursive call. I've seen this at least three times on the Developer Forums. To make things worse, if they're Objective-C refugees they probably use 'self', so they don't even see the warning normally given for this condition.

Since recursive getters and setters don't seem very useful, while accidental recursion is very common, I think they should be banned.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!