# What about a VBA style with Statement?

**URL:** <https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170>\
**Category:** Discussion\
**Created:** [April 13, 2016, 1:15pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170 "2016-04-13T13:15:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![1113](https://avatars.discourse-cdn.com/v4/letter/1/c89c15/32.png) [@1113](https://forums.swift.org/u/1113)\
**Post date:** [April 13, 2016, 1:15pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/1 "2016-04-13T13:15:08Z")

</div>

I recently learned some VBA and I found a very conveniently `with`  
statement.

`with` statement can be helpful to set property for UIKit instance.

for instance a UILabel instance `textLabel` ,with `with` statement we can  
set UILabel property like this

```swift

with textLabel{

.textAlignment = .Left

.textColor = UIColor.darkTextColor()

.font = UIFont.systemFontOfSize(15)

}

```

---

<div class="post-metadata">

**Author:** ![Radek\_Pietruszewski](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@Radek\_Pietruszewski](https://forums.swift.org/u/Radek_Pietruszewski)\
**Post date:** [April 13, 2016, 2:17pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/2 "2016-04-13T14:17:22Z")

</div>

It can be (more-or-less) solved in library code today:

extension NSObjectProtocol {  
&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

This way, you can do, on NSObjects:

textLabel.with {  
&nbsp;&nbsp;$0.textAlignment = .Left  
&nbsp;&nbsp;$0.textColor = .darkTextColor()  
}

I love this pattern.

You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).

Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.

— Radek

> **···**
>
> > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > I recently learned some VBA and I found a very conveniently `with` statement.
> > 
> > `with` statement can be helpful to set property for UIKit instance.
> > 
> > for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this
> > 
> > ```swift
> > 
> > with textLabel{
> > 
> > .textAlignment = .Left
> > 
> > .textColor = UIColor.darkTextColor()
> > 
> > .font = UIFont.systemFontOfSize(15)
> > 
> > }
> > 
> > ```
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 13, 2016, 1:35pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/3 "2016-04-13T13:35:36Z")

</div>

I like this idea.

In Delphi(Pascal) there is also such construction  
with someVar do  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;end;  
And it is very handy and is used a lot (usually if you need to set/read a lot of properties of some object).

Additionally, I believe such construction prevents some possible mistakes with used variable name, especially if there is a similar-named variables in cod (sometimes because of code completion feature in editor) ex:  
someVar1 = ...  
someVar1....

someVar2 = ..  
someVar2....  
someVar1... \<\< mistake  
someVar2...

and if "with" construction were here:

with someVar2 {  
&nbsp;&nbsp;&nbsp;.some...  
&nbsp;&nbsp;&nbsp;var a = .another  
&nbsp;&nbsp;&nbsp;etc  
}

But the problem with "with" in Delphi is that it is not explicit what method/property belongs to that someVar and what doesn't belong.  
In Swift, such construction should force us to somehow be explicit regarding what methods/props belongs to that someVar instance. Seems like "dot" syntax looks OK.

Don't know if that could be implemented in Swift technically.

> **···**
>
> On 13.04.2016 16:15, 李海珍 via swift-evolution wrote:
> 
> > I recently learned some VBA and I found a very conveniently `with` statement.
> > 
> > `with` statement can be helpful to set property for UIKit instance.
> > 
> > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > set UILabel property like this
> > 
> > ```swift
> > 
> > with textLabel{
> > 
> > .textAlignment= .Left
> > 
> > .textColor= UIColor.darkTextColor()
> > 
> > .font= UIFont.systemFontOfSize(15)
> > 
> > }
> > 
> > ```
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [April 13, 2016, 3:08pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/4 "2016-04-13T15:08:26Z")

</div>

My current version is this:

func with\<T\>(item: T, @noescape update: (inout T) -\> Void) -\> T {  
&nbsp;&nbsp;&nbsp;&nbsp;var this = item; update(&this); return this  
}

Used, for example:

struct Foo { var (a, b, c) = ("a", "b", "c") }  
class Bar: DefaultReflectable { var (a, b, c) = ("a", "b", 2.5) }  
var f = with(Foo()) { $0.a = "X" }  
var b = with(Bar()) { $0.a = "X" }  
print(f) // Foo(a: "X", b: "b", c: "c")  
print(b) // Bar(a=X, b=b, c=c)

Is there an advantage to having a pair of functions?

-- E, who moved this to Swift-Users

> **···**
>
> > On Apr 13, 2016, at 8:25 AM, Sean Heber via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > This pair works pretty well, too, if you don’t mind free functions:
> > 
> > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > 
> > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > 
> > l8r  
> > Sean

---

<div class="post-metadata">

**Author:** ![BigZaphod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bigzaphod/32/9561_2.png) [@BigZaphod](https://forums.swift.org/u/BigZaphod)\
**Post date:** [April 13, 2016, 2:25pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/5 "2016-04-13T14:25:32Z")

</div>

This pair works pretty well, too, if you don’t mind free functions:

func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }

It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.

l8r  
Sean

> **···**
>
> > On Apr 13, 2016, at 9:17 AM, Radosław Pietruszewski via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > It can be (more-or-less) solved in library code today:
> > 
> > extension NSObjectProtocol {  
> > &nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > }
> > 
> > This way, you can do, on NSObjects:
> > 
> > textLabel.with {  
> > &nbsp;&nbsp;$0.textAlignment = .Left  
> > &nbsp;&nbsp;$0.textColor = .darkTextColor()  
> > }
> > 
> > I love this pattern.
> > 
> > You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).
> > 
> > Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.
> > 
> > — Radek
> > 
> > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > 
> > > `with` statement can be helpful to set property for UIKit instance.
> > > 
> > > for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this
> > > 
> > > ```swift
> > > 
> > > with textLabel{
> > > 
> > > .textAlignment = .Left
> > > 
> > > .textColor = UIColor.darkTextColor()
> > > 
> > > .font = UIFont.systemFontOfSize(15)
> > > 
> > > }
> > > 
> > > ```
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [April 13, 2016, 3:33pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/6 "2016-04-13T15:33:39Z")

</div>

> > > This pair works pretty well, too, if you don’t mind free functions:
> > > 
> > > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > > 
> > > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > > 
> > > l8r  
> > > Sean
> > 
> > My current version is this:
> > 
> > func with\<T\>(item: T, @noescape update: (inout T) -\> Void) -\> T {  
> > &nbsp;&nbsp;&nbsp;var this = item; update(&this); return this  
> > }
> > 
> > Used, for example:
> > 
> > struct Foo { var (a, b, c) = ("a", "b", "c") }  
> > class Bar: DefaultReflectable { var (a, b, c) = ("a", "b", 2.5) }  
> > var f = with(Foo()) { $0.a = "X" }  
> > var b = with(Bar()) { $0.a = "X" }  
> > print(f) // Foo(a: "X", b: "b", c: "c")  
> > print(b) // Bar(a=X, b=b, c=c)
> > 
> > Is there an advantage to having a pair of functions?
> 
> I don’t know if it’s a huge advantage or not, but with warnings on unused results, using a with() that has a return with a class instance would mean you’d have to discard the return result explicitly or pointlessly reassign the results to your instance (thus meaning not using a let) just to avoid the warning. If you annotated the with() to allow discarding the result, then it’d be error-prone for structs. It seemed “safer” to me to have a pair.
> 
> l8r  
> Sean

If you want to call

> > let f = with(Foo()) { $0.a = "X" }  
> > let b = with(Bar()) { $0.a = "X" }

with constant assignment and an initializer, I'm not sure how yours would work. Could they?

-- E, trying right now in the playground

> **···**
>
> > On Apr 13, 2016, at 9:26 AM, Sean Heber \<sean@fifthace.com\> wrote:
> > 
> > > On Apr 13, 2016, at 10:08 AM, Erica Sadun \<erica@ericasadun.com\> wrote:
> > > 
> > > > On Apr 13, 2016, at 8:25 AM, Sean Heber via swift-evolution \<swift-evolution@swift.org\> wrote:

---

<div class="post-metadata">

**Author:** ![BigZaphod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bigzaphod/32/9561_2.png) [@BigZaphod](https://forums.swift.org/u/BigZaphod)\
**Post date:** [April 13, 2016, 3:26pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/7 "2016-04-13T15:26:09Z")

</div>

I don’t know if it’s a huge advantage or not, but with warnings on unused results, using a with() that has a return with a class instance would mean you’d have to discard the return result explicitly or pointlessly reassign the results to your instance (thus meaning not using a let) just to avoid the warning. If you annotated the with() to allow discarding the result, then it’d be error-prone for structs. It seemed “safer” to me to have a pair.

l8r  
Sean

> **···**
>
> > On Apr 13, 2016, at 10:08 AM, Erica Sadun \<erica@ericasadun.com\> wrote:
> > 
> > > On Apr 13, 2016, at 8:25 AM, Sean Heber via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > This pair works pretty well, too, if you don’t mind free functions:
> > > 
> > > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > > 
> > > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > > 
> > > l8r  
> > > Sean
> > 
> > My current version is this:
> > 
> > func with\<T\>(item: T, @noescape update: (inout T) -\> Void) -\> T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;var this = item; update(&this); return this  
> > }
> > 
> > Used, for example:
> > 
> > struct Foo { var (a, b, c) = ("a", "b", "c") }  
> > class Bar: DefaultReflectable { var (a, b, c) = ("a", "b", 2.5) }  
> > var f = with(Foo()) { $0.a = "X" }  
> > var b = with(Bar()) { $0.a = "X" }  
> > print(f) // Foo(a: "X", b: "b", c: "c")  
> > print(b) // Bar(a=X, b=b, c=c)
> > 
> > Is there an advantage to having a pair of functions?

---

<div class="post-metadata">

**Author:** ![Taras\_Zakharko1](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Taras\_Zakharko1](https://forums.swift.org/u/Taras_Zakharko1)\
**Post date:** [April 13, 2016, 2:36pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/8 "2016-04-13T14:36:15Z")

</div>

We had this kind of proposal earlier, didn’t seem to be very well received. I still think it would be a useful thing, but I’d do it as a statement (extension of do) that introduces new lexical scope (similar to self)

— Taras

> **···**
>
> > On 13 Apr 2016, at 16:25, Sean Heber via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > This pair works pretty well, too, if you don’t mind free functions:
> > 
> > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > 
> > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > 
> > l8r  
> > Sean
> > 
> > > On Apr 13, 2016, at 9:17 AM, Radosław Pietruszewski via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > It can be (more-or-less) solved in library code today:
> > > 
> > > extension NSObjectProtocol {  
> > > &nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;}  
> > > }
> > > 
> > > This way, you can do, on NSObjects:
> > > 
> > > textLabel.with {  
> > > &nbsp;&nbsp;$0.textAlignment = .Left  
> > > &nbsp;&nbsp;$0.textColor = .darkTextColor()  
> > > }
> > > 
> > > I love this pattern.
> > > 
> > > You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).
> > > 
> > > Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.
> > > 
> > > — Radek
> > > 
> > > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > > 
> > > > `with` statement can be helpful to set property for UIKit instance.
> > > > 
> > > > for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this
> > > > 
> > > > ```swift
> > > > 
> > > > with textLabel{
> > > > 
> > > > .textAlignment = .Left
> > > > 
> > > > .textColor = UIColor.darkTextColor()
> > > > 
> > > > .font = UIFont.systemFontOfSize(15)
> > > > 
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 13, 2016, 2:47pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/9 "2016-04-13T14:47:20Z")

</div>

Than you for sharing this method. So, do you suggest to not have this "with" construction in Swift just because we \*can\* implement the same behavior by using some workarounds/hacks?

I.e. do you support this proposal or don't?  
Do you agree that such built-in feature will be very useful and make our programming live a little better/easier?

I strongly believe that if such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we most likely want to have this feature as part of language rather than possibility to use some workaround to implement it.

With your suggestion, how to deal with two separate classes that have no common protocol? I.e.  
class A { .. }; class B {..};  
Should we conform them to some protocol or create extension of each just to have this "with" feature? Don't think so.

> **···**
>
> On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
> 
> > It can be (more-or-less) solved in library code today:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension NSObjectProtocol {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This way, you can do, on NSObjects:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;textLabel.with {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textAlignment = .Left
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textColor = .darkTextColor()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > I love this pattern.
> > 
> > You can also make it a function to make it work with any value of any kind  
> > (it will then take form of `with(foo) { …}`).
> > 
> > Ideally, if you could write a universal extension (something like  
> > `extension Any`), you could just add this behavior, with method syntax, to  
> > everything.
> > 
> > — Radek
> > 
> > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > 
> > > `with` statement can be helpful to set property for UIKit instance.
> > > 
> > > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > > set UILabel property like this
> > > 
> > > ```swift
> > > 
> > > with textLabel{
> > > 
> > > .textAlignment= .Left
> > > 
> > > .textColor= UIColor.darkTextColor()
> > > 
> > > .font= UIFont.systemFontOfSize(15)
> > > 
> > > }
> > > 
> > > ```
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 13, 2016, 2:56pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/10 "2016-04-13T14:56:51Z")

</div>

Yes, it really looks like language construction, and I like this approach.  
with (some) {  
&nbsp;&nbsp;&nbsp;$0... = ...  
}

But should many of us copy-paste these functions in each module/project or this is very handful feature that we want to have in our language? What do you think about the proposal?

Actually I think it is even OK if such functions will be in standard lib, i.e. without intruducing new language construct, and we can use them out-of-box to have 'with' functionality.

> **···**
>
> On 13.04.2016 17:25, Sean Heber via swift-evolution wrote:
> 
> > This pair works pretty well, too, if you don’t mind free functions:
> > 
> > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > 
> > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > 
> > l8r  
> > Sean
> > 
> > > On Apr 13, 2016, at 9:17 AM, Radosław Pietruszewski via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > It can be (more-or-less) solved in library code today:
> > > 
> > > extension NSObjectProtocol {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > }
> > > 
> > > This way, you can do, on NSObjects:
> > > 
> > > textLabel.with {  
> > > &nbsp;&nbsp;$0.textAlignment = .Left  
> > > &nbsp;&nbsp;$0.textColor = .darkTextColor()  
> > > }
> > > 
> > > I love this pattern.
> > > 
> > > You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).
> > > 
> > > Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.
> > > 
> > > — Radek
> > > 
> > > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > > 
> > > > `with` statement can be helpful to set property for UIKit instance.
> > > > 
> > > > for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this
> > > > 
> > > > ```swift
> > > > 
> > > > with textLabel{
> > > > 
> > > > .textAlignment = .Left
> > > > 
> > > > .textColor = UIColor.darkTextColor()
> > > > 
> > > > .font = UIFont.systemFontOfSize(15)
> > > > 
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 22, 2016, 7:18am UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/11 "2016-04-22T07:18:41Z")

</div>

Just wanted to summarize our opinions on this suggestion that we was discussing earlier and check if someone is ready to crate an "official" proposal for this feature.

There a number of questions regarding this feature we can discuss, for example statement vs method, what could be a placeholder of target instance inside the scope($0, $, \_, .., nothing etc)

The main question, \*do you support that we need "with" feature in some way in Swift 3.0 out of the box\* . And if so, what variant do you prefer.

\* this proposal is for \*explicit\* "with", where it is clear what method/property belongs to the "target" instance  
\* I believe that as such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we want to have this feature as part of language/standard lib rather than possibility to use some workaround to implement it  
\* It is not about saving the space in code. It is about more readable and (I insist) more stable(with less errors) code. Much less possibilities for copy-paste errors. Wrong code completion suggestion(by editor) can not produce error. It is explicit and clear, it has much less noise in code.  
\* Many of us already implemented and use such "with" construction in some way

There were 2 main suggestions :

1) Introduce "with" statement that can be used in for example in such way:

// set props just after creating  
// similar to "if let.. " and "guard let.."  
with let questionLabel = UILabel() {  
&nbsp;&nbsp;&nbsp;//set props of created instance here  
&nbsp;&nbsp;&nbsp;// here we can have:  
&nbsp;&nbsp;&nbsp;// $0.prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// ..prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// .prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// \_.prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// $.prop = value  
&nbsp;&nbsp;&nbsp;// or ?  
}  
// questionLabel is available here

// works for structures  
with var some = SomeStruct() {  
&nbsp;&nbsp;&nbsp;//...  
}

// just for some class/structure/enum  
with questionLabel {  
&nbsp;&nbsp;&nbsp;// ..  
}

probably

with var src = someNamedInstance1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let dst = someNamedInstance2 {  
&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
}

or  
with someNamedInstance1, someNamedInstance2 {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.propA = $1.propB  
&nbsp;&nbsp;&nbsp;&nbsp;$1.someMethod($0.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;$0.someMehtod()  
}

2) Introduce .with method for each(?) class/struct, so we can use out-of-box:

let questionLabel = UILabel().with {  
&nbsp;&nbsp;&nbsp;//set props of created instance here  
&nbsp;&nbsp;&nbsp;$0.prop = value  
}

var someStructInstance = SomeStruct().with {target in  
&nbsp;&nbsp;&nbsp;target.prop = value  
}

questionLabel.with {label in  
&nbsp;&nbsp;&nbsp;label.prop = value  
}

someNamedInstance1.with(someNamedInstance2) {src, dst in  
&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
}

Note that function like this :  
func with\<T\>(item:T, apply:(T)-\>Void) { apply(item) }

Produces such kind of problems:  
struct A {var x = 1}  
let a1 = A() // constant  
with (a1) { $0.x = 10 } // this will be compiled without errors/warnings

> **···**
>
> On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
> 
> > It can be (more-or-less) solved in library code today:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension NSObjectProtocol {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This way, you can do, on NSObjects:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;textLabel.with {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textAlignment = .Left
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textColor = .darkTextColor()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > I love this pattern.
> > 
> > You can also make it a function to make it work with any value of any kind  
> > (it will then take form of `with(foo) { …}`).
> > 
> > Ideally, if you could write a universal extension (something like  
> > `extension Any`), you could just add this behavior, with method syntax, to  
> > everything.
> > 
> > — Radek
> > 
> > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > 
> > > `with` statement can be helpful to set property for UIKit instance.
> > > 
> > > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > > set UILabel property like this
> > > 
> > > ```swift
> > > 
> > > with textLabel{
> > > 
> > > .textAlignment= .Left
> > > 
> > > .textColor= UIColor.darkTextColor()
> > > 
> > > .font= UIFont.systemFontOfSize(15)
> > > 
> > > }
> > > 
> > > ```
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Radek\_Pietruszewski](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@Radek\_Pietruszewski](https://forums.swift.org/u/Radek_Pietruszewski)\
**Post date:** [April 13, 2016, 3:09pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/12 "2016-04-13T15:09:37Z")

</div>

> Than you for sharing this method. So, do you suggest to not have this "with" construction in Swift just because we \*can\* implement the same behavior by using some workarounds/hacks?
> 
> I.e. do you support this proposal or don't?  
> Do you agree that such built-in feature will be very useful and make our programming live a little better/easier?

I’m -1, at least in the foreseeable future. I do agree that this is a useful construct, but if I can do it in library code, paying only a small price for this, I’d prefer Swift to grow better in places that a library \*can’t\* fix.

> I strongly believe that if such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we most likely want to have this feature as part of language rather than possibility to use some workaround to implement it.
> 
> With your suggestion, how to deal with two separate classes that have no common protocol? I.e.  
> class A { .. }; class B {..};  
> Should we conform them to some protocol or create extension of each just to have this "with" feature? Don't think so.

No — for now, it’s best to use a free function for now. And, like I mentioned, universal conformances could allow this to be easily added to all types — and so I’d focus on pushing \*that\* proposal.

> **···**
>
> > On 13 Apr 2016, at 16:47, Vladimir.S \<svabox@gmail.com\> wrote:
> 
> > On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
> > 
> > > It can be (more-or-less) solved in library code today:
> > > 
> > > &nbsp;&nbsp;&nbsp;extension NSObjectProtocol {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > This way, you can do, on NSObjects:
> > > 
> > > &nbsp;&nbsp;&nbsp;textLabel.with {
> > > 
> > > &nbsp;&nbsp;&nbsp;$0.textAlignment = .Left
> > > 
> > > &nbsp;&nbsp;&nbsp;$0.textColor = .darkTextColor()
> > > 
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > I love this pattern.
> > > 
> > > You can also make it a function to make it work with any value of any kind  
> > > (it will then take form of `with(foo) { …}`).
> > > 
> > > Ideally, if you could write a universal extension (something like  
> > > `extension Any`), you could just add this behavior, with method syntax, to  
> > > everything.
> > > 
> > > — Radek
> > > 
> > > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \>\>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\> \<mailto:swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\>\> wrote:
> > > > 
> > > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > > 
> > > > `with` statement can be helpful to set property for UIKit instance.
> > > > 
> > > > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > > > set UILabel property like this
> > > > 
> > > > ```swift
> > > > 
> > > > with textLabel{
> > > > 
> > > > .textAlignment= .Left
> > > > 
> > > > .textColor= UIColor.darkTextColor()
> > > > 
> > > > .font= UIFont.systemFontOfSize(15)
> > > > 
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\> \<mailto:swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 13, 2016, 3:17pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/13 "2016-04-13T15:17:02Z")

</div>

+1 on statement vs lexical scope. additionally { $0.something ... } is more explicit than just .something.

> **···**
>
> On 13.04.2016 17:36, Taras Zakharko via swift-evolution wrote:
> 
> > We had this kind of proposal earlier, didn’t seem to be very well received. I still think it would be a useful thing, but I’d do it as a statement (extension of do) that introduces new lexical scope (similar to self)
> > 
> > — Taras
> > 
> > > On 13 Apr 2016, at 16:25, Sean Heber via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > This pair works pretty well, too, if you don’t mind free functions:
> > > 
> > > func with\<T\>(inout this: T, @noescape using: inout T-\>Void) { using(&this) }  
> > > func with\<T\>(this: T, @noescape using: T-\>Void) { using(this) }
> > > 
> > > It works either with classes or mutable structs if you call it correctly and the type doesn’t matter.
> > > 
> > > l8r  
> > > Sean
> > > 
> > > > On Apr 13, 2016, at 9:17 AM, Radosław Pietruszewski via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > It can be (more-or-less) solved in library code today:
> > > > 
> > > > extension NSObjectProtocol {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > }
> > > > 
> > > > This way, you can do, on NSObjects:
> > > > 
> > > > textLabel.with {  
> > > > &nbsp;&nbsp;$0.textAlignment = .Left  
> > > > &nbsp;&nbsp;$0.textColor = .darkTextColor()  
> > > > }
> > > > 
> > > > I love this pattern.
> > > > 
> > > > You can also make it a function to make it work with any value of any kind (it will then take form of `with(foo) { …}`).
> > > > 
> > > > Ideally, if you could write a universal extension (something like `extension Any`), you could just add this behavior, with method syntax, to everything.
> > > > 
> > > > — Radek
> > > > 
> > > > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > > 
> > > > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > > > 
> > > > > `with` statement can be helpful to set property for UIKit instance.
> > > > > 
> > > > > for instance a UILabel instance `textLabel` ,with `with` statement we can set UILabel property like this
> > > > > 
> > > > > ```swift
> > > > > 
> > > > > with textLabel{
> > > > > 
> > > > > .textAlignment = .Left
> > > > > 
> > > > > .textColor = UIColor.darkTextColor()
> > > > > 
> > > > > .font = UIFont.systemFontOfSize(15)
> > > > > 
> > > > > }
> > > > > 
> > > > > ```
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 13, 2016, 4:54pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/14 "2016-04-13T16:54:36Z")

</div>

> I’m -1, at least in the foreseeable future. I do agree that this is a  
> useful construct, but if I can do it in library code, paying only a small  
> price for this, I’d prefer Swift to grow better in places that a library  
> \*can’t\* fix.

Well, there a lot of things that \*your personal\* library can fix for you.  
Should we stop improve the language and start to write just personal libs with improvements?  
This proposals about the feature that makes Swift better, more enjoyable to work with.

Btw, this "with" method you suggest is not replacement for language construction - it allows compilation when struct instance constant (let s = SomeStruct()) is changed inside block. Do you like this? Do you want to produce such an non-safe code to your projects?

> No — for now, it’s best to use a free function for now. And, like I

No, not best, even not OK. Described above.

> mentioned, universal conformances could allow this to be easily added to  
> all types — and so I’d focus on pushing \*that\* proposal.

As I understand, such a method will have the same problem with un-safe behavior in case of constant.

> **···**
>
> On 13.04.2016 18:09, Radosław Pietruszewski wrote:

---

<div class="post-metadata">

**Author:** ![Patrick\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/patrick_smith/32/3428_2.png) [@Patrick\_Smith](https://forums.swift.org/u/Patrick_Smith)\
**Post date:** [April 22, 2016, 8:12am UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/15 "2016-04-22T08:12:46Z")

</div>

I was wondering if all mutating and setters of a method could become a type (Typename.Mutation), say something similar to an enum.  
You’d use it like this:  
let alice = john mutating [.firstName = "Alice", .makeScottishClan]  
See more here:[https://gist.github.com/BurntCaramel/ba2ce9dfd49595dacce07394de579172](https://gist.github.com/BurntCaramel/ba2ce9dfd49595dacce07394de579172)  
Patrick

Just wanted to summarize our opinions on this suggestion that we was  
discussing earlier and check if someone is ready to crate an "official"  
proposal for this feature.

There a number of questions regarding this feature we can discuss, for  
example statement vs method, what could be a placeholder of target instance  
inside the scope($0, $, \_, .., nothing etc)

The main question, \*do you support that we need "with" feature in some way  
in Swift 3.0 out of the box\* . And if so, what variant do you prefer.

\* this proposal is for \*explicit\* "with", where it is clear what  
method/property belongs to the "target" instance  
\* I believe that as such feature is really useful and handy, if it is  
explicit and if it is clearly showing in code what we are doing - we want  
to have this feature as part of language/standard lib rather than  
possibility to use some workaround to implement it  
\* It is not about saving the space in code. It is about more readable and  
(I insist) more stable(with less errors) code. Much less possibilities for  
copy-paste errors. Wrong code completion suggestion(by editor) can not  
produce error. It is explicit and clear, it has much less noise in code.  
\* Many of us already implemented and use such "with" construction in some way

There were 2 main suggestions :

1) Introduce "with" statement that can be used in for example in such way:

// set props just after creating  
// similar to "if let.. " and "guard let.."  
with let questionLabel = UILabel() {  
&nbsp;&nbsp;&nbsp;//set props of created instance here  
&nbsp;&nbsp;&nbsp;// here we can have:  
&nbsp;&nbsp;&nbsp;// $0.prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// ..prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// .prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// \_.prop = value  
&nbsp;&nbsp;&nbsp;// or  
&nbsp;&nbsp;&nbsp;// $.prop = value  
&nbsp;&nbsp;&nbsp;// or ?  
}  
// questionLabel is available here

// works for structures  
with var some = SomeStruct() {  
&nbsp;&nbsp;&nbsp;//...  
}

// just for some class/structure/enum  
with questionLabel {  
&nbsp;&nbsp;&nbsp;// ..  
}

probably

with var src = someNamedInstance1,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let dst = someNamedInstance2 {  
&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
}

or  
with someNamedInstance1, someNamedInstance2 {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.propA = $1.propB  
&nbsp;&nbsp;&nbsp;&nbsp;$1.someMethod($0.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;$0.someMehtod()  
}

2) Introduce .with method for each(?) class/struct, so we can use out-of-box:

let questionLabel = UILabel().with {  
&nbsp;&nbsp;&nbsp;//set props of created instance here  
&nbsp;&nbsp;&nbsp;$0.prop = value  
}

var someStructInstance = SomeStruct().with {target in  
&nbsp;&nbsp;&nbsp;target.prop = value  
}

questionLabel.with {label in  
&nbsp;&nbsp;&nbsp;label.prop = value  
}

someNamedInstance1.with(someNamedInstance2) {src, dst in  
&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
}

Note that function like this :  
func with(item:T, apply:(T)-\>Void) { apply(item) }

Produces such kind of problems:  
struct A {var x = 1}  
let a1 = A() // constant  
with (a1) { $0.x = 10 } // this will be compiled without errors/warnings

> **···**
>
> On Fri, Apr 22, 2016 at 12:18 AM -0700, "Vladimir.S via swift-evolution" \<swift-evolution@swift.org\> wrote:
> 
> On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
> 
> > It can be (more-or-less) solved in library code today:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension NSObjectProtocol {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This way, you can do, on NSObjects:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;textLabel.with {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textAlignment = .Left
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;$0.textColor = .darkTextColor()
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > I love this pattern.
> > 
> > You can also make it a function to make it work with any value of any kind  
> > (it will then take form of `with(foo) { …}`).
> > 
> > Ideally, if you could write a universal extension (something like  
> > `extension Any`), you could just add this behavior, with method syntax, to  
> > everything.
> > 
> > — Radek
> > 
> > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \>\> \> wrote:
> > > 
> > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > 
> > > `with` statement can be helpful to set property for UIKit instance.
> > > 
> > > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > > set UILabel property like this
> > > 
> > > ```swift
> > > 
> > > with textLabel{
> > > 
> > > .textAlignment= .Left
> > > 
> > > .textColor= UIColor.darkTextColor()
> > > 
> > > .font= UIFont.systemFontOfSize(15)
> > > 
> > > }
> > > 
> > > ```
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> swift-evolution mailing list  
> swift-evolution@swift.org  
> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![VladimirS](https://avatars.discourse-cdn.com/v4/letter/v/f9ae1b/32.png) [@VladimirS](https://forums.swift.org/u/VladimirS)\
**Post date:** [April 22, 2016, 10:27am UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/16 "2016-04-22T10:27:24Z")

</div>

Personally, I don't feel like your suggestion is a replacement for "with" feature. "With" not just about mutating(it can be used to just get multiply props from "target" instance), it allows to call methods of instance, use a label of "target" instance (like $0) to be used as argument (like with someInstance { ...; callFunc($0); } etc.

Also, in your suggestion there is implicit "self" for the target instance (.firstName) and this will produce not obvious code (like .someColor = .colorRed - first belongs to target instance, second - enum value)

I believe you can start new [Discussion]/[Idea] about this "mutating" feature, in this thread I'd like to collect opinions regarding the "with" feature proposal.

> **···**
>
> On 22.04.2016 11:12, Patrick Smith wrote:
> 
> > I was wondering if all mutating and setters of a method could become a type  
> > (Typename.Mutation), say something similar to an enum.
> > 
> > You’d use it like this:
> > 
> > let alice = john mutating [  
> > &nbsp;&nbsp;.firstName = "Alice",  
> > &nbsp;&nbsp;.makeScottishClan  
> > ]
> > 
> > See more here:  
> > [https://gist.github.com/BurntCaramel/ba2ce9dfd49595dacce07394de579172](https://gist.github.com/BurntCaramel/ba2ce9dfd49595dacce07394de579172)
> > 
> > Patrick
> > 
> > On Fri, Apr 22, 2016 at 12:18 AM -0700, "Vladimir.S via swift-evolution" \> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Just wanted to summarize our opinions on this suggestion that we was  
> > &nbsp;&nbsp;&nbsp;&nbsp;discussing earlier and check if someone is ready to crate an "official"  
> > &nbsp;&nbsp;&nbsp;&nbsp;proposal for this feature.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;There a number of questions regarding this feature we can discuss, for  
> > &nbsp;&nbsp;&nbsp;&nbsp;example statement vs method, what could be a placeholder of target instance  
> > &nbsp;&nbsp;&nbsp;&nbsp;inside the scope($0, $, \_, .., nothing etc)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;The main question, \*do you support that we need "with" feature in some way  
> > &nbsp;&nbsp;&nbsp;&nbsp;in Swift 3.0 out of the box\* . And if so, what variant do you prefer.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;\* this proposal is for \*explicit\* "with", where it is clear what  
> > &nbsp;&nbsp;&nbsp;&nbsp;method/property belongs to the "target" instance  
> > &nbsp;&nbsp;&nbsp;&nbsp;\* I believe that as such feature is really useful and handy, if it is  
> > &nbsp;&nbsp;&nbsp;&nbsp;explicit and if it is clearly showing in code what we are doing - we want  
> > &nbsp;&nbsp;&nbsp;&nbsp;to have this feature as part of language/standard lib rather than  
> > &nbsp;&nbsp;&nbsp;&nbsp;possibility to use some workaround to implement it  
> > &nbsp;&nbsp;&nbsp;&nbsp;\* It is not about saving the space in code. It is about more readable and  
> > &nbsp;&nbsp;&nbsp;&nbsp;(I insist) more stable(with less errors) code. Much less possibilities for  
> > &nbsp;&nbsp;&nbsp;&nbsp;copy-paste errors. Wrong code completion suggestion(by editor) can not  
> > &nbsp;&nbsp;&nbsp;&nbsp;produce error. It is explicit and clear, it has much less noise in code.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\* Many of us already implemented and use such "with" construction in some way
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;There were 2 main suggestions :
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;1) Introduce "with" statement that can be used in for example in such way:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;// set props just after creating  
> > &nbsp;&nbsp;&nbsp;&nbsp;// similar to "if let.. " and "guard let.."  
> > &nbsp;&nbsp;&nbsp;&nbsp;with let questionLabel = UILabel() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//set props of created instance here  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// here we can have:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// $0.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// or  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ..prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// or  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// .prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// or  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// \_.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// or  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// $.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// or ?  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;// questionLabel is available here
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;// works for structures  
> > &nbsp;&nbsp;&nbsp;&nbsp;with var some = SomeStruct() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//...  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;// just for some class/structure/enum  
> > &nbsp;&nbsp;&nbsp;&nbsp;with questionLabel {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ..  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;probably
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;with var src = someNamedInstance1,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let dst = someNamedInstance2 {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;or  
> > &nbsp;&nbsp;&nbsp;&nbsp;with someNamedInstance1, someNamedInstance2 {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$0.propA = $1.propB  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$1.someMethod($0.propC)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$0.someMehtod()  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;2) Introduce .with method for each(?) class/struct, so we can use out-of-box:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;let questionLabel = UILabel().with {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//set props of created instance here  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$0.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var someStructInstance = SomeStruct().with {target in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;target.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;questionLabel.with {label in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label.prop = value  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;someNamedInstance1.with(someNamedInstance2) {src, dst in  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src.propA = dst.propB  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst.someMethod(src.propC)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src.someMehtod()  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Note that function like this :  
> > &nbsp;&nbsp;&nbsp;&nbsp;func with(item:T, apply:(T)-\>Void) { apply(item) }
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;Produces such kind of problems:  
> > &nbsp;&nbsp;&nbsp;&nbsp;struct A {var x = 1}  
> > &nbsp;&nbsp;&nbsp;&nbsp;let a1 = A() // constant  
> > &nbsp;&nbsp;&nbsp;&nbsp;with (a1) { $0.x = 10 } // this will be compiled without errors/warnings
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> It can be (more-or-less) solved in library code today:  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> extension NSObjectProtocol {  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> public func with(@noescape fn: Self -\> Void) -\> Self {  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> fn(self)  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> return self  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> }  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> }  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> This way, you can do, on NSObjects:  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> textLabel.with {  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> $0.textAlignment = .Left  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> $0.textColor = .darkTextColor()  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> }  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> I love this pattern.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> You can also make it a function to make it work with any value of any kind  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> (it will then take form of `with(foo) { …}`).  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> Ideally, if you could write a universal extension (something like  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> `extension Any`), you could just add this behavior, with method syntax, to  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> everything.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> — Radek  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \> \>\> \> wrote:  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> I recently learned some VBA and I found a very conveniently `with` statement.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> `with` statement can be helpful to set property for UIKit instance.  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> set UILabel property like this  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> ```swift  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> with textLabel{  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> .textAlignment= .Left  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> .textColor= UIColor.darkTextColor()  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> .font= UIFont.systemFontOfSize(15)  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> }  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> ```  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> swift-evolution mailing list  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> swift-evolution@swift.org  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> swift-evolution mailing list  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> swift-evolution@swift.org  
> > &nbsp;&nbsp;&nbsp;&nbsp;\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > &nbsp;&nbsp;&nbsp;&nbsp;\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > &nbsp;&nbsp;&nbsp;&nbsp;swift-evolution mailing list  
> > &nbsp;&nbsp;&nbsp;&nbsp;swift-evolution@swift.org  
> > &nbsp;&nbsp;&nbsp;&nbsp;[https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![trs](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/trs/32/2038_2.png) [@trs](https://forums.swift.org/u/trs)\
**Post date:** [April 23, 2016, 11:28am UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/17 "2016-04-23T11:28:37Z")

</div>

> Note that function like this :  
> func with\<T\>(item:T, apply:(T)-\>Void) { apply(item) }
> 
> Produces such kind of problems:  
> struct A {var x = 1}  
> let a1 = A() // constant  
> with (a1) { $0.x = 10 } // this will be compiled without errors/warnings

This works as expected (giving a compile error for let constant):

// for value types  
func with\<T\>(inout item: T, apply: (inout T) throws -\> Void) rethrows -\> Void {  
&nbsp;&nbsp;&nbsp;&nbsp;try apply(&item)  
}

// for reference types (classes)  
func with\<T: AnyObject\>(item: T, apply: (T) throws -\> Void) rethrows -\> T {  
&nbsp;&nbsp;&nbsp;&nbsp;try apply(item)  
&nbsp;&nbsp;&nbsp;&nbsp;return item  
}

// value types

struct A { var x = 1 }

var a1 = A()  
with (&a1) {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.x = 10  
}  
print(a1) // A(x: 10)

/\*  
let a2 = A()  
with (&a2) { // error: cannot pass immutable value as inout argument: 'a2' is a 'let' constant  
&nbsp;&nbsp;&nbsp;&nbsp;$0.x = 10  
}  
print(a2)  
\*/

// reference types (classes)

class B : CustomDebugStringConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;var y = 1  
&nbsp;&nbsp;&nbsp;&nbsp;var debugDescription: String { return "B(y: \(y))" }  
}

var b1 = B()  
with (b1) {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.y = 11  
}  
print(b1) // B(y: 11)

// reference types allow this pattern (value types don't)  
let b2 = with (B()) {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.y = 12  
}  
print(b2) // B(y: 12)

-Thorsten

> **···**
>
> > Am 22.04.2016 um 09:18 schrieb Vladimir.S via swift-evolution \<swift-evolution@swift.org\>:
> > 
> > Just wanted to summarize our opinions on this suggestion that we was discussing earlier and check if someone is ready to crate an "official" proposal for this feature.
> > 
> > There a number of questions regarding this feature we can discuss, for example statement vs method, what could be a placeholder of target instance inside the scope($0, $, \_, .., nothing etc)
> > 
> > The main question, \*do you support that we need "with" feature in some way in Swift 3.0 out of the box\* . And if so, what variant do you prefer.
> > 
> > \* this proposal is for \*explicit\* "with", where it is clear what method/property belongs to the "target" instance  
> > \* I believe that as such feature is really useful and handy, if it is explicit and if it is clearly showing in code what we are doing - we want to have this feature as part of language/standard lib rather than possibility to use some workaround to implement it  
> > \* It is not about saving the space in code. It is about more readable and (I insist) more stable(with less errors) code. Much less possibilities for copy-paste errors. Wrong code completion suggestion(by editor) can not produce error. It is explicit and clear, it has much less noise in code.  
> > \* Many of us already implemented and use such "with" construction in some way
> > 
> > There were 2 main suggestions :
> > 
> > 1) Introduce "with" statement that can be used in for example in such way:
> > 
> > // set props just after creating  
> > // similar to "if let.. " and "guard let.."  
> > with let questionLabel = UILabel() {  
> > //set props of created instance here  
> > // here we can have:  
> > // $0.prop = value  
> > // or  
> > // ..prop = value  
> > // or  
> > // .prop = value  
> > // or  
> > // \_.prop = value  
> > // or  
> > // $.prop = value  
> > // or ?  
> > }  
> > // questionLabel is available here
> > 
> > // works for structures  
> > with var some = SomeStruct() {  
> > //...  
> > }
> > 
> > // just for some class/structure/enum  
> > with questionLabel {  
> > // ..  
> > }
> > 
> > probably
> > 
> > with var src = someNamedInstance1,  
> > &nbsp;&nbsp;&nbsp;&nbsp;let dst = someNamedInstance2 {  
> > &nbsp;&nbsp;src.propA = dst.propB  
> > &nbsp;&nbsp;dst.someMethod(src.propC)  
> > &nbsp;&nbsp;src.someMehtod()  
> > }
> > 
> > or  
> > with someNamedInstance1, someNamedInstance2 {  
> > &nbsp;&nbsp;$0.propA = $1.propB  
> > &nbsp;&nbsp;$1.someMethod($0.propC)  
> > &nbsp;&nbsp;$0.someMehtod()  
> > }
> > 
> > 2) Introduce .with method for each(?) class/struct, so we can use out-of-box:
> > 
> > let questionLabel = UILabel().with {  
> > //set props of created instance here  
> > $0.prop = value  
> > }
> > 
> > var someStructInstance = SomeStruct().with {target in  
> > target.prop = value  
> > }
> > 
> > questionLabel.with {label in  
> > label.prop = value  
> > }
> > 
> > someNamedInstance1.with(someNamedInstance2) {src, dst in  
> > &nbsp;&nbsp;src.propA = dst.propB  
> > &nbsp;&nbsp;dst.someMethod(src.propC)  
> > &nbsp;&nbsp;src.someMehtod()  
> > }
> > 
> > Note that function like this :  
> > func with\<T\>(item:T, apply:(T)-\>Void) { apply(item) }
> > 
> > Produces such kind of problems:  
> > struct A {var x = 1}  
> > let a1 = A() // constant  
> > with (a1) { $0.x = 10 } // this will be compiled without errors/warnings
> > 
> > On 13.04.2016 17:17, Radosław Pietruszewski via swift-evolution wrote:
> > 
> > > It can be (more-or-less) solved in library code today:
> > > 
> > > &nbsp;&nbsp;&nbsp;extension NSObjectProtocol {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func with(@noescape fn: Self -\> Void) -\> Self {  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn(self)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > This way, you can do, on NSObjects:
> > > 
> > > &nbsp;&nbsp;&nbsp;textLabel.with {
> > > 
> > > &nbsp;&nbsp;&nbsp;$0.textAlignment = .Left
> > > 
> > > &nbsp;&nbsp;&nbsp;$0.textColor = .darkTextColor()
> > > 
> > > &nbsp;&nbsp;&nbsp;}
> > > 
> > > I love this pattern.
> > > 
> > > You can also make it a function to make it work with any value of any kind  
> > > (it will then take form of `with(foo) { …}`).
> > > 
> > > Ideally, if you could write a universal extension (something like  
> > > `extension Any`), you could just add this behavior, with method syntax, to  
> > > everything.
> > > 
> > > — Radek
> > > 
> > > > On 13 Apr 2016, at 15:15, 李海珍 via swift-evolution \>\>\> \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > I recently learned some VBA and I found a very conveniently `with` statement.
> > > > 
> > > > `with` statement can be helpful to set property for UIKit instance.
> > > > 
> > > > for instance a UILabel instance `textLabel` ,with `with` statement we can  
> > > > set UILabel property like this
> > > > 
> > > > ```swift
> > > > 
> > > > with textLabel{
> > > > 
> > > > .textAlignment= .Left
> > > > 
> > > > .textColor= UIColor.darkTextColor()
> > > > 
> > > > .font= UIFont.systemFontOfSize(15)
> > > > 
> > > > }
> > > > 
> > > > ```
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Kurt\_Werle](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@Kurt\_Werle](https://forums.swift.org/u/Kurt_Werle)\
**Post date:** [April 13, 2016, 3:34pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/18 "2016-04-13T15:34:49Z")

</div>

I've always thought that the with construct is not a good idea. It seems  
to me that the moment you want to use with it indicates that you are  
getting too much into some other classes business; shouldn't that class  
deal with this? Why are you exposing/integrating so much of some other  
class's logic? Maybe there should be a method that does all this, or maybe  
a custom struct that passes all the appropriate information...

Yeah, there are exceptions - always. But for the most part I'm not a fan.

Kurt

> **···**
>
> --  
> kurt@CircleW.org
> 
> > **[kurt - Circle W](http://www.circlew.org/kurt/)**
> >
> > Kurt & Ellyn

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [April 13, 2016, 4:04pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/19 "2016-04-13T16:04:56Z")

</div>

The biggest advantage of the with pattern IMO is Cocoa initializers. It provides a more unified  
initialization scope. Instead of:

let questionLabel = UILabel()  
questionLabel.textAlignment = .Center  
questionLabel.font = UIFont(name:"DnealianManuscript", size: 72)  
questionLabel.text = currentQuestion.text  
questionLabel.numberOfLines = 0

You get:

let questionLabel2 = with(UILabel()) {  
&nbsp;&nbsp;&nbsp;&nbsp;$0.textAlignment = .Center  
&nbsp;&nbsp;&nbsp;&nbsp;$0.font = UIFont(name:"DnealianManuscript", size: 72)  
&nbsp;&nbsp;&nbsp;&nbsp;$0.text = currentQuestion.text  
&nbsp;&nbsp;&nbsp;&nbsp;$0.numberOfLines = 0  
}

You also have the option to customize a Value type before assigning it to a constant or to base  
a new value constant on a copy of an existing value constant:

struct Foo { var (a, b, c) = ("a", "b", "c") }  
var f = with(Foo()) { $0.a = "X" }  
print(f) // Foo(a: "X", b: "b", c: "c")

I personally would love to see a Swift construct that created a scope with `self` defined so the  
awkward `$0.` prefixes could be discarded.

-- E, who may have brought up this topic once or twice before on this list

> **···**
>
> > On Apr 13, 2016, at 9:34 AM, Kurt Werle via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > I've always thought that the with construct is not a good idea. It seems to me that the moment you want to use with it indicates that you are getting too much into some other classes business; shouldn't that class deal with this? Why are you exposing/integrating so much of some other class's logic? Maybe there should be a method that does all this, or maybe a custom struct that passes all the appropriate information...
> > 
> > Yeah, there are exceptions - always. But for the most part I'm not a fan.
> > 
> > Kurt

---

<div class="post-metadata">

**Author:** ![Taras\_Zakharko1](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Taras\_Zakharko1](https://forums.swift.org/u/Taras_Zakharko1)\
**Post date:** [April 13, 2016, 3:40pm UTC](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170/20 "2016-04-13T15:40:32Z")

</div>

Because many APIs use configuration/partial initialisation patterns. Look at Metal for example. But I agree that most of the time, its not such a big deal.

— Taras

> **···**
>
> > On 13 Apr 2016, at 17:34, Kurt Werle \<kurt@circlew.org\> wrote:
> > 
> > I've always thought that the with construct is not a good idea. It seems to me that the moment you want to use with it indicates that you are getting too much into some other classes business; shouldn't that class deal with this? Why are you exposing/integrating so much of some other class's logic? Maybe there should be a method that does all this, or maybe a custom struct that passes all the appropriate information...
> > 
> > Yeah, there are exceptions - always. But for the most part I'm not a fan.
> > 
> > Kurt  
> > --  
> > kurt@CircleW.org  
> > [Welcome to circlew.org](http://www.CircleW.org/kurt/) \<[Welcome to circlew.org](http://www.circlew.org/kurt/&gt);

[Next page](https://forums.swift.org/t/what-about-a-vba-style-with-statement/2170.md?page=2)
