<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Why you have to use `<span style="font-size:12.800000190734863px;font-family:arial,sans-serif">unwrapped optional` at the first place? If you have to use it, it means it could be nil. So it won't conform the protocol, which requires the `value` never nil.</span></div><div class="gmail_default" style="font-family:georgia,serif"><span style="font-family:Menlo;font-size:11px;font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)"><br></span></div><div class="gmail_default"><font face="georgia, serif" color="#000000">Zhaoxin</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, May 29, 2017 at 12:37 PM, Anton Bronnikov via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi All,<div><br></div><div>If I have a protocol with a property requirement such as:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(4,51,255)"><span style="font-variant-ligatures:no-common-ligatures">protocol</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> Foo {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">var</span><span style="font-variant-ligatures:no-common-ligatures"> value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Int</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">get</span><span style="font-variant-ligatures:no-common-ligatures"> }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div></blockquote><div><br></div><div>.. and I need to conform to it using an implicitly unwrapped optional like:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">struct</span><span style="font-variant-ligatures:no-common-ligatures"> Bar : </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Foo</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">let</span><span style="font-variant-ligatures:no-common-ligatures"> value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Int</span><span style="font-variant-ligatures:no-common-ligatures">! = </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">nil</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div></blockquote><div><br></div><div>.. then the compiler fails with an error:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>Playground execution failed: error: scratchpad.playground:5:8: error: type 'Bar' does not conform to protocol 'Foo'</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>struct Bar : Foo {</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b> ^</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>scratchpad.playground:2:9: note: protocol requires property 'value' with type 'Int'; do you want to add a stub?</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b> var value: Int { get }</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b> ^</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b>scratchpad.playground:6:9: note: candidate has non-matching type 'Int!'</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b> let value: Int! = nil</b></span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"><b> ^</b></span></div></div></blockquote><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"><b></b></span></div></div><div>Technically, I understand why the error, and currently work around it with a cumbersome:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">struct</span><span style="font-variant-ligatures:no-common-ligatures"> Bar : </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Foo</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">var</span><span style="font-variant-ligatures:no-common-ligatures"> value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Int</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">_value</span><span style="font-variant-ligatures:no-common-ligatures">! }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">let</span><span style="font-variant-ligatures:no-common-ligatures"> _value: </span><span style="font-variant-ligatures:no-common-ligatures;color:#3495af">Int</span><span style="font-variant-ligatures:no-common-ligatures">? = </span><span style="font-variant-ligatures:no-common-ligatures;color:#0433ff">nil</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div></blockquote><div><br></div><div>However, I keep wondering - would it not be making sense to accept implicitly unwrapped optionals as a conformance to be base-type requirements? It sort of works just like that in all other parts of the language.</div><div><br></div><div>Or in other words, is this by design, or should I probably create an issue for this?</div><div><br></div><div>Thank you.</div><div>Cheers,</div><div>Anton</div><div><br></div><div>P.S. The example is oversimplified for the sake of clarity, in practice the property just has to be an optional - implicitly unwrapped or not - because it’s a part of the composite that holds unowning reference to its container object.</div><div><br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>