Re: [swift-users] still wrestling with a type conversion problem

2017-07-05 Thread David Baraff via swift-users
> func decodeMe(_ payload:Any) -> T? { > if let dt = T.self as? DecodableFromAny { > return type(of: dt).fromAny(payload) as! T > } else { > return payload as? T > } > } Auggh! I only thought it worked. Luckily, I had help. My 16 year-old son looked at this code,

Re: [swift-users] still wrestling with a type conversion problem

2017-07-05 Thread David Baraff via swift-users
> On Jul 4, 2017, at 5:48 PM, Jacob Bandes-Storch wrote: > > On Tue, Jul 4, 2017 at 7:21 AM, David Baraff > wrote: > > func decoded(_ input: Any) -> Set { > if let listVal = input as? [Set.Element] { >

Re: [swift-users] still wrestling with a type conversion problem

2017-07-04 Thread Jacob Bandes-Storch via swift-users
On Tue, Jul 4, 2017 at 7:21 AM, David Baraff wrote: > > func decoded(_ input: Any) -> Set { > if let listVal = input as? [Set.Element] { > return Set(listVal) > } > return self > } > > This looks a little weird — what is `self`

Re: [swift-users] still wrestling with a type conversion problem

2017-07-03 Thread Jacob Bandes-Storch via swift-users
Using a protocol with an "as?" cast convinces the compiler to do a dynamic lookup for a protocol conformance based on the value's actual type: protocol MyEncodable { func encoded() -> Any } func encode(_ value: T) -> Any { if let value = value as? MyEncodable {

[swift-users] still wrestling with a type conversion problem

2017-07-03 Thread David Baraff via swift-users
I’m trying to provide some custom serialization from within a generic class. (Briefly put, I want to automatically convert types like Set to an array upon serialization, and the reverse when I read it back.) While trying things out, I was suprised by this: public func encode(_ value:T) -> Any {