[swift-evolution] What about a VBA style with Statement?

Vladimir.S svabox at gmail.com
Wed Apr 13 11:32:40 CDT 2016


But in case of struct instance constant - we have a problem:

struct A {
     var x = 1
     var y = 2
}

let a1 = A()

with (a1) {
     print($0.x)
     $0.y = 10
}

- this will be compiled without errors/warnings, but yes - there will be 
runtime error. I'm sure this is not what we need from Swift. And so, this 
"with" function can not be a 100% replacement for special language 
construction. So, all, please provide your opinion on this proposal and I 
believe we should move it forward.


On 13.04.2016 18:51, Erica Sadun via swift-evolution wrote:
>
>> On Apr 13, 2016, at 7:50 AM, Benzi via swift-evolution
>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> The closest right now you can achieve would be:
>>
>> func with<T>(item:T, apply:(T)->Void) {
>>     apply(item)
>> }
>>
>>
>> let label = UILabel()
>> label.highlighted // defaults to false
>>
>> with(label) {
>>     $0.highlighted = true
>> }
>>
>> label.highlighted // is now true
>
> As a reference type, you can just go ahead and assign:
>
> letlabel  = UILabel()
> label.highlighted= true
>
> -- E
>
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list