<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;">Hello Swift community, I’d like to throw a few more ideas on what features future Swift version might or should have.</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;">I’d like to talk about implicit protocols for reference and value types first and then about type oriented protocols.</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;">As everyone know Swift has a great differentiation between reference types (only class) and value types (struct, enum and tuples).</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;">From my point of view Swift still lacks of implicit protocols for these types. Don’t get me wrong, we already have `AnyObject`, but this protocol is a bit tricky, because whenever you use it in your generic function, you "might“ bridge your type to an Objective-C type.</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;">In one of my little projects I could build a workaround, but this wasn’t on parameter level as I'd like it to be:</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;">So I used something like this inside the function scope to differentiate between reference or value types.</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;">guard instance.dynamicType is AnyClass else { return }</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;">This won’t bridge my type to an Objective-C type, but I will know that it’s a class type.</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;">Lets sum up what we already have:</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;">`Any = protocol<>` - The protocol to which all types implicitly conform.</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;">`@objc AnyObject` - The protocol to which all classes implicitly conform.</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;">That been said I’d like to introduce you some new protocols I’d wish Swift to have.</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;">protocol ReferenceType {} or protocol AnyReference {} - The protocol to which all (Swift) classes implicitly conform.</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;">and</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;">protocol ValueType {} or protocol AnyValue {} - The protocol to which all (Swift) value types implicitly conform.</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;">Also it would be good that `AnyObject` would extend from `ReferenceType / AnyReference` protocol.</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;">This will open a few new doors for us to build more specific code. For example we could finally differentiate between value and reference types and overload functions correctly for a specific desire. </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;">Here is an abstract example:</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;"><div id="bloop_customfont" style="margin: 0px;">func mutate<T: AnyValue>(value: T, scope: @noescape (value: inout T) -> Void) -> T {</div><div id="bloop_customfont" style="margin: 0px;"><span class="Apple-tab-span" style="white-space:pre">        </span></div><div id="bloop_customfont" style="margin: 0px;"> var mutableValue = value</div><div id="bloop_customfont" style="margin: 0px;"> scope(value: &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;">func mutate<T: AnyReference>(value: T, scope: @noescape (value: T) -> Void) -> T {</div><div id="bloop_customfont" style="margin: 0px;"><span class="Apple-tab-span" style="white-space:pre">        </span></div><div id="bloop_customfont" style="margin: 0px;"> scope(value: value)</div><div id="bloop_customfont" style="margin: 0px;"> return value</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;">class A { var x = 0 }</div><div id="bloop_customfont" style="margin: 0px;">struct B { var y = 1 }</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">let a = mutate(A()) { $0.x = 10 }</div><div id="bloop_customfont" style="margin: 0px;">let b = mutate(B()) { $0.y = 42 }</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">That’s all I have on my mind about implicit protocols right now. Now I’d like to scratch the surface of type oriented protocols.</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;"><div id="bloop_customfont" style="margin: 0px;">We already have the ability to create class only protocols like so:</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">protocol SomeName: class {}</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">And there is no chance to limit these protocols to value types only, so a library user might be forced to design value types for some purpose.</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">Does Swift need something like `protocol SomeName: struct {}` or `protocol SomeName: value {}`? What do you think?</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">If we already had `AnyReference` and `AnyValue` protocols we could drop the `class` keyword for protocols and use `AnyReference` instead. This will also open us the way for creating value types only protocols like so: `protocol ValueProtocol: AnyValue {}`.</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">Migrating wouldn’t be that hard, because we only have to replace the `class` keyword on protocols with the new `AnyReference` protocol.</div><div id="bloop_customfont" style="margin: 0px;"><br></div><div id="bloop_customfont" style="margin: 0px;">Im curious about your feedback.</div><div><br></div></div></div><br><div id="bloop_sign_1461396776461100032" class="bloop_sign"><div>-- <br>Adrian Zubarev</div></div></body></html>