Proposal: Auto-implemented computed properties


(Nathan Yee) #1

Certain languages allow the programmer to avoid creating backing variables
for getters and setters by using this syntax:

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double {
        get
        set
    }
    // alternatively var x: Double { get; set }
}

and generating code equivalent to this:

class Foo {
    init(x: Double) {
        _x = x
    }
    var _x: Double
    var x: Double {
        get {
            return _x
        }
        set {
            _x = newValue
        }
    }
}

This notation decreases verbosity and reduces the chance of incorrectly
implementing the pattern.

In the following case, the computed property 'x' can only be set in the
initializer.

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double { get }
}

Alternatively, the following syntax can be used to avoid using an
initializer:

class Foo {
    var x: Double { get } = 1.0
}

Before looking into the nuances of this syntax (regarding struct/enum
properties, access control, attributes, etc.) I would like to ask the
community if this feature would be a good fit for Swift.

···

--
Nathan


(Chris Lattner) #2

Certain languages allow the programmer to avoid creating backing variables for getters and setters by using this syntax:

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double {
        get
        set
    }
    // alternatively var x: Double { get; set }
}

and generating code equivalent to this:

class Foo {
    init(x: Double) {
        _x = x
    }
    var _x: Double
    var x: Double {
        get {
            return _x
        }
        set {
            _x = newValue
        }
    }
}

Hi Nathan,

How is this different than just declaring “var x : Double”? It is an important part of swift’s design that stored and computed properties are equivalent to external uses of an API.

-Chris

···

On Dec 11, 2015, at 11:20 AM, Nathan Yee via swift-evolution <swift-evolution@swift.org> wrote:

This notation decreases verbosity and reduces the chance of incorrectly implementing the pattern.

In the following case, the computed property 'x' can only be set in the initializer.

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double { get }
}

Alternatively, the following syntax can be used to avoid using an initializer:

class Foo {
    var x: Double { get } = 1.0
}

Before looking into the nuances of this syntax (regarding struct/enum properties, access control, attributes, etc.) I would like to ask the community if this feature would be a good fit for Swift.

--
Nathan
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


#3

Why not just use `let`?

Stephen

···

On Fri, Dec 11, 2015 at 2:20 PM, Nathan Yee via swift-evolution < swift-evolution@swift.org> wrote:

In the following case, the computed property 'x' can only be set in the
initializer.

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double { get }
}


(Nicky Gerritsen) #4

I’m wondering what the actual use of this is. Why not just write:

class Foo {
  init(x: Double) {
    self.x = x
  }

  var x: Double
}

Regards,

Nicky

···

On 11 dec. 2015, at 20:20, Nathan Yee via swift-evolution <swift-evolution@swift.org> wrote:

Certain languages allow the programmer to avoid creating backing variables for getters and setters by using this syntax:

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double {
        get
        set
    }
    // alternatively var x: Double { get; set }
}

and generating code equivalent to this:

class Foo {
    init(x: Double) {
        _x = x
    }
    var _x: Double
    var x: Double {
        get {
            return _x
        }
        set {
            _x = newValue
        }
    }
}

This notation decreases verbosity and reduces the chance of incorrectly implementing the pattern.

In the following case, the computed property 'x' can only be set in the initializer.

class Foo {
    init(x: Double) {
        self.x = x
    }
    var x: Double { get }
}

Alternatively, the following syntax can be used to avoid using an initializer:

class Foo {
    var x: Double { get } = 1.0
}

Before looking into the nuances of this syntax (regarding struct/enum properties, access control, attributes, etc.) I would like to ask the community if this feature would be a good fit for Swift.

--
Nathan
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution