Yes, it really looks like language construction, and I like this approach.
with (some) {
  $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 {
     public func with(@noescape fn: Self -> Void) -> Self {
         fn(self)
         return self
     }
}

This way, you can do, on NSObjects:

textLabel.with {
        $0.textAlignment = .Left
        $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

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to