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

David Baraff davidbaraff at gmail.com
Tue Jul 4 09:21:18 CDT 2017


I can’t thank you enough for that.

I totally missed that you can impart behavior to all the types that are part of a generic type by doing a protocol on the generic type itself (i.e. extension Set: MyEncodable).

This approach is really what I wanted because it lets an outside user create their own type and then make it be serializable without *me* (as the author of this facility) knowing they’ve done that.

——————————————
(after some implementation)

extension Set: MyEncodable {
    func encoded() -> Any {
        return self.map { $0 }
    }
    
    func decoded(_ input: Any) -> Set {
        if let listVal = input as? [Set.Element] {
            return Set(listVal)
        }
        return self
    }
}


C++ blows.  Swift rocks.

Thank you again.




More information about the swift-users mailing list