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

John McCall rjmccall at apple.com
Mon Mar 13 11:40:44 CDT 2017


> On Mar 11, 2017, at 11:57 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> On Mar 10, 2017, at 9:33 PM, David Sweeris via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Is this a feature or a bug?
> 
> Low priority bug IMO.

Agreed.

John.

> 
> -Chris
> 
> 
>> 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
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170313/5e9ec3fc/attachment.html>


More information about the swift-evolution mailing list