<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You can just remove </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div style="font-family:arial,sans-serif;font-size:13px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:rgb(0,84,147)">    public</span> <span style="color:rgb(0,84,147)">init</span>(<span style="color:rgb(0,84,147)">_</span> a: <span style="color:rgb(52,56,146)">T</span>) {</div></div><div style="font-family:arial,sans-serif;font-size:13px"><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;">        <span style="color:rgb(129,65,0)">actual</span> = a</div></div><div><div style="font-size:11px;font-family:&#39;Fira Code&#39;;margin:0px;line-height:normal">    }</div><div style="font-size:11px;font-family:&#39;Fira Code&#39;;margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">It is redundant. Below code works.</font></div><div style="font-size:11px;font-family:&#39;Fira Code&#39;;margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal"><p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">public</span><span style=""> </span><span style="color:rgb(187,44,162)">struct</span><span style=""> check&lt;T&gt; {</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> actual: </span><span style="color:rgb(79,129,135)">T</span><span style="">?</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="">//    </span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="">//    public init(_ a: T) {</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="">//        actual = a</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(0,132,0)"><span style="">//    }</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style="">    </span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">public</span><span style=""> </span><span style="color:rgb(187,44,162)">init</span><span style="">(</span><span style="color:rgb(187,44,162)">_</span><span style=""> a: </span><span style="color:rgb(79,129,135)">T</span><span style="">?) {</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">        </span><span style="color:rgb(79,129,135)">actual</span><span style=""> = a</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">    }</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style="">    </span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">public</span><span style=""> </span><span style="color:rgb(187,44,162)">var</span><span style=""> isNil: </span><span style="color:rgb(112,61,170)">Bool</span><span style=""> {</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">        </span><span style="color:rgb(187,44,162)">return</span><span style=""> </span><span style="color:rgb(79,129,135)">actual</span><span style=""> == </span><span style="color:rgb(187,44,162)">nil</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">    }</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="">}</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style=""></span><br></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">let</span><span style=""> str: </span><span style="color:rgb(112,61,170)">String</span><span style=""> = </span><span style="color:rgb(209,47,27)">&quot;Hi&quot;</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="">check</span><span style="color:rgb(0,0,0)">(</span><span style="">str</span><span style="color:rgb(0,0,0)">).</span><span style="">isNil</span><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">                </span></span><span style="color:rgb(0,132,0)">// false</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style=""></span><br></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">let</span><span style=""> maybe: </span><span style="color:rgb(112,61,170)">String</span><span style="">? = </span><span style="color:rgb(187,44,162)">nil</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="">check</span><span style="color:rgb(0,0,0)">(</span><span style="">maybe</span><span style="color:rgb(0,0,0)">).</span><span style="">isNil</span><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">                </span></span><span style="color:rgb(0,132,0)">// true</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;min-height:13px"><span style=""></span><br></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">let</span><span style=""> hmm: </span><span style="color:rgb(187,44,162)">Any</span><span style="">? = </span><span style="color:rgb(187,44,162)">nil</span></p>
<p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="">check</span><span style="color:rgb(0,0,0)">(</span><span style="">hmm</span><span style="color:rgb(0,0,0)">).</span><span style="">isNil</span><span style="color:rgb(0,0,0)"><span class="" style="white-space:pre">                </span></span><span style="color:rgb(0,132,0)">// true</span></p><p style="font-size:11px;font-family:Menlo;margin:0px;line-height:normal;color:rgb(79,129,135)"><span style="color:rgb(0,132,0)"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">Zhaoxin</font></p></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Aug 29, 2016 at 11:39 PM, Jens Alfke via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</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"><div>I’m making a little utility struct that needs to be able to wrap an arbitrary value, which may be an optional, and be able to tell whether it’s nil. So I declared it like this (I’ve removed the interesting/useful parts of the code leaving just the constructors and one property):</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:&#39;Fira Code&#39;"><span style="color:#005493">public</span><span> </span><span style="color:#005493">struct</span><span> check&lt;T&gt; {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:rgb(0,84,147)">let</span><span> actual: </span><span style="color:rgb(52,56,146)">T</span><span>?</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#005493">public</span><span> </span><span style="color:#005493">init</span><span>(</span><span style="color:#005493">_</span><span> a: </span><span style="color:#343892">T</span><span>) {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>        </span><span style="color:#814100">actual</span><span> = a</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:#005493">public</span><span> </span><span style="color:#005493">init</span><span>(</span><span style="color:#005493">_</span><span> a: </span><span style="color:#343892">T</span><span>?) {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>        </span><span style="color:#814100">actual</span><span> = a</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    </span><span style="color:rgb(0,84,147)">public</span><span> </span><span style="color:rgb(0,84,147)">var</span><span> isNil: </span><span style="color:rgb(112,61,170)">Bool</span><span> {</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>        </span><span style="color:#005493">return</span><span> </span><span style="color:#814100">actual</span><span> == </span><span style="color:#005493">nil</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>    }</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span>}</span></div></div></blockquote><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br><span></span></div></div><div>This works fine, except when I try to use it with a value of type `Any?`:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:rgb(0,84,147)">let</span><span> str: </span><span style="color:rgb(112,61,170)">String</span><span> = </span><span style="color:rgb(121,121,121)">&quot;Hi&quot;</span></div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(129,65,0)"><span style="color:rgb(52,56,146)">check</span><span style="color:rgb(0,0,0)">(</span><span>str</span><span style="color:rgb(0,0,0)">).</span><span>isNil<span style="white-space:pre-wrap">                </span>// false</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:rgb(0,84,147)">let</span><span> maybe: </span><span style="color:rgb(112,61,170)">String</span><span>? = </span><span style="color:rgb(0,84,147)">nil</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;color:rgb(129,65,0)"><span style="color:rgb(52,56,146)">check</span><span style="color:rgb(0,0,0)">(</span><span>maybe</span><span style="color:rgb(0,0,0)">).</span><span>isNil<span style="white-space:pre-wrap">                </span>// true</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;;min-height:13px"><br></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:rgb(0,84,147)">let</span><span> hmm: </span><span style="color:rgb(0,84,147)">Any</span><span>? = </span><span style="color:rgb(0,84,147)">nil</span></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:&#39;Fira Code&#39;"><span style="color:rgb(52,56,146)">check</span><span>(</span><span style="color:rgb(129,65,0)">hmm</span><span>).isNil<span style="white-space:pre-wrap">                </span>// Error: “Ambiguous use of ‘init&#39;&quot;</span></div></div></blockquote><div><br></div><div>(I agree that `Any?` is sort of a weird type; in my code it represents a value that might be a JSON object. NSJSONSerialization parses to type `Any`, but there might not be a value, so it becomes an Optional&lt;Any&gt; or Any?.)</div><div><br></div><div>This worked in Swift 2.2, but after upgrading to Swift 3 [in the latest Xcode 8 beta] the compiler started complaining.</div><div><br></div><div>How can I fix the `check` struct&#39;s constructor(s) to make this work?</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>—Jens</div></font></span></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>