<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="">I figure out how to do this right. Something like:</div><div class=""><br class=""></div><div class="">protocol DecodableFromAny {<br class=""> static func fromAny(_ payload:Any) -> Self?<br class="">}<br class=""><br class="">extension Array : DecodableFromAny {<br class=""> static func fromAny(_ payload: Any) -> Array? {<br class=""> if let x = payload as? [Array.Element] {<br class=""> return x<br class=""> }<br class=""> return nil<br class=""> }<br class="">}<br class=""><br class="">func decodeMe<T>(_ payload:Any) -> T? {<br class=""> if let dt = T.self as? DecodableFromAny {<br class=""> return type(of: dt).fromAny(payload) as! T<br class=""> } else {<br class=""> return payload as? T<br class=""> }<br class="">}<br class=""><br class="">I got tripped up not realizing I could cast “T.self” to the protocol, and then call it once i invoked type(of:) on that. much much nicer this way.</div><div class=""><br class=""></div></body></html>