[swift-evolution] Assignment to 'let' constant in defer blocks

David Sweeris davesweeris at mac.com
Fri Mar 10 23:33:12 CST 2017


Is this a feature or a bug?
class Foo {
    let bar: Int
    init?(someConditionBasedOnInputData: Bool) {
        var localBar: Int = 0
        defer {
            bar = localBar //Cannot assign to property: 'bar' is a 'let' constant
        }
        if someConditionBasedOnInputData {
            return nil
        }
    }
}
It’d be handy to be able to do the assignment upfront in a defer block in cases where there’s both a bunch of validation to do on the input data, and no harm from assigning the interim values to the final values when you’re about to return nil anyway.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170310/34f849b2/attachment.html>


More information about the swift-evolution mailing list