[swift-users] [swift-evolution] What about a VBA style with Statement?
Erica Sadun
erica at ericasadun.com
Wed Apr 13 10:08:26 CDT 2016
> On Apr 13, 2016, at 8:25 AM, Sean Heber via swift-evolution <swift-evolution at 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 {
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160413/bba73aae/attachment.html>
More information about the swift-users
mailing list