<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Ooops, my bad, I didn’t scroll your reply to the very end and so I didn’t read your full answer.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">The custom `with` statement with infix function was just an example that it might be possible to build.</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">It won’t look that 'hacky‘ if Swift would have an explicit protocol for all value types. Something like `AnyValueType`</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Then we could easily overload such a custom infix function</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div>infix func with<T: AnyValueType>(lhs: T, rhs: (inout T) -> Void) -> T {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>var mutableValue = lhs</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>rhs(&mutableValue)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return mutableValue</div><div>}</div><div><br></div><div>infix func with<T: AnyObject>(lhs: T, rhs: (T) -> Void) -> T {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>rhs(lhs)</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return lhs</div><div>}</div><div><br></div><div>There is also no chance of such usage, because the `with` function is marked as `infix`</div><div><br></div><div>struct B { var x = 10 }</div><div><br></div><div>let b = B()</div><div><br></div><div>with(b) { print($0.x) } // this won’t be possible at all, infix operators doesn’t allow that so why should infix functions?</div><div><br></div><div>As expected we will be able to do something like this:</div><div><br></div><div>let newImutableB = B() with {</div><div><br></div><div> $0.x = 40 // mutation here is allowed, because we copy internally and path the new value back</div><div>}</div><div><br></div><div>Infix functions should forbid reserved functions names like `if`, `do`, `else` etc.</div><div><br></div><div><br></div> <div id="bloop_sign_1461356996110285056" class="bloop_sign"><div>-- <br>Adrian Zubarev</div></div> <br><p class="airmail_on">Am 22. April 2016 bei 22:15:26, Adrian Zubarev (<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div></div><div>
<title></title>
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<div id="bloop_customfont" style="margin: 0px;">I know that this
might be a bit confusing but at least for a syntax highlighting
editor this shouldn’t be a problem. Any custom function is
displayed with a different color (at least Xcode do).</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">For structs one
could use:</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">
<div id="bloop_customfont" style="margin: 0px;">struct B { var x =
10 }</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">func
with<T>(lhs: T, rhs: (inout T) -> Void) -> T {</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;"></div>
<div id="bloop_customfont" style="margin: 0px;"> var
mutableValue = lhs</div>
<div id="bloop_customfont" style="margin: 0px;">
rhs(&mutableValue)</div>
<div id="bloop_customfont" style="margin: 0px;"> return
mutableValue</div>
<div id="bloop_customfont" style="margin: 0px;">}</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">let value = B()
with {</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;"></div>
<div id="bloop_customfont" style="margin: 0px;"> $0.x =
40</div>
<div id="bloop_customfont" style="margin: 0px;">}</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">This does even
supports Swifty mutation if needed.</div>
<div id="bloop_customfont" style="margin: 0px;"><br></div>
<div id="bloop_customfont" style="margin: 0px;">`with` statement is
just a single example we could create with infix functions.</div>
<div><br></div>
</div>
</div>
<br>
<div id="bloop_sign_1461356100088779008" class="bloop_sign">
<div>-- <br>
Adrian Zubarev</div>
</div>
<br>
<p class="airmail_on">Am 22. April 2016 bei 22:05:11, Vladimir.S
via swift-evolution (<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>)
schrieb:</p>
<blockquote type="cite" class="clean_bq">
<div>
<div><span>On 22.04.2016 22:06, Adrian Zubarev via swift-evolution
wrote:<br>
> I’d like to throw an idea in the room and see where this will
go.<br>
><br>
> What if Swift would allow us to create custom infix
functions?<br>
> Does Swift need to ability of infix functions?<br>
> How powerful can such a feature be?<br>
> Pros and cons?<br>
><br>
> There is a discussion about the `with` statement which we
could develop<br>
> with infix functions like so:<br>
<br>
IMO Such construction will looks like a language construction and
will<br>
confuse.<br>
<br>
And the function you suggest as replacement for "with" feature will
*not*<br>
correctly handle struct instances (Swift 3.0 mar 24):<br>
<br>
struct B { var x = 10 }<br>
<br>
func with<T: AnyObject>(lhs: T, rhs: @noescape (T) ->
Void) { rhs(lhs) }<br>
<br>
var b2 = B()<br>
with(b2) { print($0.x) }<br>
<br>
It will produce this error :<br>
--------------<br>
Error running code:<br>
l-value expression does not have l-value access kind set<br>
...<br>
-------------<br>
<br>
This is why I propose to introduce standard "with" feature(language
feature<br>
or built-in method(s)/free function(s)) in Swift out-of-box i.e.
tested and<br>
well-working solution in any situation. Not some hack/workaround
that each<br>
one will write for himself and that will not work in some
situation.<br>
<br>
I can implement "with" method in some way for struct that will work
like this:<br>
<br>
struct B {<br>
var x = 10<br>
<br>
mutating func withInPlace(user: @noescape (inout B)->Void) ->
B {<br>
var copy = self<br>
user(&copy)<br>
self = copy<br>
return self<br>
}<br>
<br>
func with(user: @noescape (inout B)->Void) -> B {<br>
var copy = self<br>
user(&copy)<br>
return copy<br>
}<br>
}<br>
<br>
var b1 = B().with {<br>
$0.x = 100<br>
}<br>
<br>
print("created b1.x = ", b1.x)<br>
<br>
b1.withInPlace { $0.x = 1000 }<br>
<br>
let c1 = b1.with {<br>
print("b1.x = ", $0.x)<br>
$0.x = 2000<br>
}<br>
<br>
print("b1.x = ", b1.x)<br>
print("c1.x = ", c1.x)<br>
<br>
But again, this is custom solution, we need standard and tested way
to do this.<br>
<br>
<br>
><br>
> infix func with<T: AnyObject>(lhs: T, rhs: @noescape (T)
-> Void) {<br>
><br>
> rhs(lhs)<br>
> }<br>
><br>
> class A {<br>
><br>
> func foo() {}<br>
> }<br>
><br>
> let instance: A = A() with {<br>
><br>
> $0.foo()<br>
> }<br>
><br>
> So what does the Swift community think about that idea?<br>
><br>
> --<br>
> Adrian Zubarev<br>
><br>
><br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> swift-evolution@swift.org<br>
> https://lists.swift.org/mailman/listinfo/swift-evolution<br>
><br>
_______________________________________________<br>
swift-evolution mailing list<br>
swift-evolution@swift.org<br>
https://lists.swift.org/mailman/listinfo/swift-evolution<br></span></div>
</div>
</blockquote>
</div></div></span></blockquote></body></html>