What about a VBA style with Statement?

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:

let label = UILabel()
label.highlighted = true

-- E

···

On Apr 13, 2016, at 7:50 AM, Benzi via swift-evolution <swift-evolution@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

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@swift.org <mailto:swift-evolution@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

My version of `with` doesn't have this issue. However, it's a clear logical error on the programmer's part when you treat a value type as a reference type.

-- E

···

On Apr 13, 2016, at 10:32 AM, Vladimir.S <svabox@gmail.com> wrote:

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.

logical error on the programmer's part when you treat a value type as a reference type.

Could you provide your 'with' function please?(checked last emails - can't find) Right now it is not clear for me how this problem could be resolved.

As for your notes about "value/reference".. Could you describe with more details?
In my example, I want to show that with this 'with' function you can compile code that tries to change constant of value(struct) type. Let say just by mistake, i.e. initially you wanted to just get values of this struct constant.
So this 'with' function should be treated as hack, not as a good solution for 'with' feature. (yes, for reference type it should work OK)

···

On 13.04.2016 19:48, Erica Sadun wrote:
> My version of `with` doesn't have this issue. However, it's a clear