<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;"><blockquote type="cite" class="clean_bq">As we can(as example) expect that in 3rd party code someone will do: <br><br>extension StructA: ExtraProtocol1 { <br>func bar() {} <br>} <br><br>extension StructB: ExtraProtocol2 { <br>func blort() {} <br>} </blockquote></div> <div><br></div>Can we really do that? I mean, I thought about that myself but I came to the conclusion that this scenario is like: I was to lazy to couple this structs to my library protocols, will you do that for me?<div><br></div><div>Sure one could think that this protocols might be optional but the `<span style="font-family: 'helvetica Neue', helvetica;">f(p: MyProtocol)</span>` function will cover this scenario.</div><div><br></div><div>Another interesting side-effect `struct<>`, `class<>` and `enum<>` will allow us to do is to distinguish between value and reference types for generics. I tried this differentiation types with protocols like `AnyReference` and `AnyValue` in another topic before (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160502/016286.html"><span style="color: rgb(0, 0, 0);">Should we rename "class" when referring to protocol conformance?</span></a>), but I kinda like this new approach.</div><div><br></div><div>Here is what I mean in detail:</div><div><br></div><div>protocol SomeProtocol /* we can’t constraint it to value types at the moment, only `class`es works */ {}</div><div><br></div><div>func foo<T>(value: struct<T, SomeProtocol>) { /* do some work */ }</div><div><br></div><div>This function is pretty neat. (1) You can force the library user to create a struct with conformance to `SomeProtocol`. (2) This approach will accept any struct which conforms to that protocol.</div><div><br></div><div>As I said in the protocol comment above protocols can only be constrained to classes at the moment, and this might change in the future. If we also had some sort of things for generics so the function from above might have looked like this:</div><div><br></div><div>func foo<T: struct where T: SomeProtocol>(value: T) {}</div><div><br></div><div>But it seems that such a thing won’t ever happen to Swift.</div><div><br></div><div>Basically `struct<>`, `class<>` and `enum<>` will just enable this for us. `all<>` would accept any type at its first element.</div><div><br></div><div>func foo<T /* add more constraints here */ >(value: all<T, SomeProtocol>) { /* T could be a reference type or value type */ }</div><div><br></div><div>That been said, `all<>` could replace `protocol<>` where it is composed from protocols. `all<>` can only be used as a generic constraints if the first element is a protocol or a reference type.</div><div><br></div><div>@Matthew: isn’t this somehow a step towards (generic) `PureValue` types?</div><div><br></div><div>struct A<T> {</div><div><br></div><div> var value: struct<T> // if we drop the strict rule of at least one protocols</div><div>}</div><div><br></div><div>How does it sound to you?</div><div> </div><div> <div id="bloop_sign_1463164748581245952" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div> <br><p class="airmail_on">Am 13. Mai 2016 bei 20:34:59, Vladimir.S (<a href="mailto:svabox@gmail.com">svabox@gmail.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>Hmm..
<br>
<br>What about such synthetic scenario:
<br>
<br>at the moment of writing our code we have:
<br>
<br>public protocol MyProtocol {
<br> func foo()
<br>}
<br>
<br>public struct StructA:MyProtocol {
<br> func foo()
<br>}
<br>
<br>public struct StructB:MyProtocol {
<br> func foo()
<br>}
<br>
<br>and have
<br>
<br>public protocol ExtraProtocol1 {
<br> func bar()
<br>}
<br>
<br>public protocol ExtraProtocol2 {
<br> func blort()
<br>}
<br>
<br>then we actually can have such code:
<br>
<br>func f(p: MyProtocol) {
<br> if let a = p as? struct<StructA, ExtraProtocol1> {
<br> a.foo()
<br> a.bar()
<br> }
<br> else
<br> if let b = p as? struct<StructB, ExtraProtocol2> {
<br> b.foo()
<br> b.blort()
<br> }
<br>}
<br><br>
<br>
<br>
<br>On 13.05.2016 20:50, Adrian Zubarev via swift-evolution wrote:
<br>>> 'struct<>' does seem redundant unless it becomes subtypeable. If
<br>>> you want a struct which conforms to several protocols, protocol<>
<br>>> already covers this.
<br>>>
<br>>> I think this is not correct. Lets check this example:
<br>>>
<br>>> func foo(value: SomeProtocol) {
<br>>>
<br>>> if let a = value as? struct<StructA, SomeProtocol> { /* do
<br>>> something with a */ }
<br>>>
<br>>> else if let b = value as? struct<StructB, SomeProtocol> { /* do
<br>>> something with b */ }
<br>>>
<br>>> }
<br>>>
<br>>> In this scenario you’ll be able to access properties and functions
<br>>> from `StructA` or `StructB` which might not be covered by
<br>>> `SomeProtocol`. Everything is merged nicely into one instance. But
<br>>> you are right it depends on the use-case.
<br>>>
<br>>>
<br>>> There is no need to include the protocol here. Just do this:
<br>>>
<br>>> if let a = value as? StructA { use a }
<br>>>
<br>> Whoops, I forgot that this will do the trick. I apologize for any confusion
<br>> here, you are totally right.
<br>>
<br>> That been said, do we really need `type<>` aka. `all<>` for value types? I
<br>> need to rethink this part of the proposal. Is there any use-case where we
<br>> would need this (any scenario for the future Swift version also counts)?
<br>>
<br>> If we had `all<>` in Swift already for extendable reference types and one
<br>> day structs would become subtypeable, this wouldn’t be a huge problem to
<br>> upgrade `all<>` for structs I guess.
<br>>
<br>> --
<br>> Adrian Zubarev
<br>> Sent with Airmail
<br>>
<br>>
<br>>
<br>> _______________________________________________
<br>> swift-evolution mailing list
<br>> swift-evolution@swift.org
<br>> https://lists.swift.org/mailman/listinfo/swift-evolution
<br>>
<br></div></div></span></blockquote></div></body></html>