<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 4, 2017, at 5:48 PM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class=""><div class="m_-1260016577223262920gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div class="">On Tue, Jul 4, 2017 at 7:21 AM, David Baraff <span dir="ltr" class=""><<a href="mailto:davidbaraff@gmail.com" target="_blank" class="">davidbaraff@gmail.com</a>></span> wrote:<br class=""></div></div></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
func decoded(_ input: Any) -> Set {<br class="">
if let listVal = input as? [Set.Element] {<br class="">
return Set(listVal)<br class="">
}<br class="">
return self<br class="">
}<br class=""><br class=""></blockquote><div class=""><br class=""></div><div class="">This looks a little weird — what is `self` here? It might be more appropriate to use a `static func` or even a failable `init?`.</div></div></div></div>
</div></blockquote></div><br class=""><div class="">Yes, it is weird. What’s going on is this: we invoke the decoded function in the protocol by writing</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>defaultValue.decoded(someObscureAny)</div><div class=""><br class=""></div><div class="">in this case, we’re saying “take my Any, and give me back the same type of thing as defaultValue, but if you can’t, then give me back defaultValue”. So now you’ve imparted the type information of defaultValue to be able to call the correct function. And i agree this is strange. i’d much rather have</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>static func decoded(_ input:Any, defaultValue:Set) -> Set {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">but when I tried that, I got a compiler error about needing to do something which has type constraints. I think a static function in the protocol doesn’t lock things down enough?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>