<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 Mar 23, 2016, at 11:29 AM, Joanna Carter <<a href="mailto:joanna@carterconsulting.org.uk" class="">joanna@carterconsulting.org.uk</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Hi Joe<br class=""><br class=""><blockquote type="cite" class=""><br class="">You can accomplish this with Swift today by casting your Any.Type to a Protocol.Type that provides an initializer:<br class=""><br class="">protocol Deserializable {<br class=""> init(deserializedFrom stream: DeserializationStream)<br class="">}<br class=""><br class="">func deserializeValue(type type: Any.Type, from stream: DeserializationStream) -> Any? {<br class=""> if let deserializableType = type as? Deserializable.Type {<br class=""> return deserializableType.init(deserializedFrom: stream)<br class=""> }<br class=""> return nil<br class="">}<br class=""></blockquote><br class="">Hmm… I've been playing with this for days now and, as useful as your code is for instantiating a given type, what I need to do is instantiate a generic type, bound to that given type.<br class=""><br class="">Something along the lines of…<br class=""><br class="">public protocol PropertyProtocol<br class="">{<br class=""> var untypedValue: Any? { get }<br class="">}<br class=""><br class="">public struct Property<PropertyType : Any><br class="">{<br class=""> public let info: PropertyInfo<br class=""><br class=""> public var name: String<br class=""> {<br class=""> return info.name<br class=""> }<br class=""><br class=""> public var displayName: String<br class=""> {<br class=""> return info.displayName<br class=""> }<br class=""><br class=""> public var value: PropertyType?<br class=""><br class=""> public init()<br class=""> {<br class=""> self.init(propertyInfo: PropertyInfo(), value: nil)<br class=""> }<br class=""><br class=""> init(propertyInfo: PropertyInfo, value: PropertyType?)<br class=""> {<br class=""> self.value = value<br class=""><br class=""> <a href="http://self.info" class="">self.info</a> = propertyInfo;<br class=""> }<br class=""><br class=""> init(propertyInfo: PropertyInfo)<br class=""> {<br class=""> self.init(propertyInfo: propertyInfo, value: nil)<br class=""> }<br class=""><br class=""> init(other: Property<PropertyType>)<br class=""> {<br class=""> self.init(propertyInfo: <a href="http://other.info" class="">other.info</a>, value: other.value)<br class=""> }<br class="">}<br class=""><br class="">struct PropertyFactory<br class="">{<br class=""> static func createBoundPropertywithValueType(valueType: Any.Type) -> PropertyProtocol<br class=""> {<br class=""> return Property<valueType>.init()<br class=""> }<br class="">}<br class=""><br class="">Of course, Ive still got a lot of C# cruft in my thinking so I am more than willing to admit I may not be approaching this in the right way ;-)<br class=""></div></div></blockquote></div><br class=""><div class="">Sorry for not getting back to you sooner. What's necessary here is a way to "open" the type of Any.Type, turning it back into something the compiler considers as a type instead of a value. This is something we've discussed having better support for, but right now there's only a sneaky way to do it using protocol extensions. If you constrain Property's type parameter to a protocol:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">protocol PropertyType {}</div><div class="">struct Property<T: PropertyType>: PropertyProtocol {}</div></blockquote><div class=""><br class=""></div><div class="">then you can add a static method to PropertyType in an extension, which can use Self as the opened dynamic type:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">extension PropertyType {</div><div class=""> private static func create() -> Property<Self> {</div><div class=""> return Property<Self>.init()</div><div class=""> }</div><div class="">}</div></blockquote><div class=""><br class=""></div><div class="">and delegate to that extension method in your factory function:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">struct PropertyFactory {</div><div class=""> static func createBoundPropertywithValueType(valueType: PropertyType.Type) -> PropertyProtocol</div><div class=""> {<br class=""> return valueType.create()<br class=""> }</div><div class="">}</div><div class=""><br class=""></div></blockquote>-Joe<br class=""><div class=""><br class=""></div><div class=""><blockquote type="cite" class="">struct PropertyFactory<br class="">{<br class=""> static func createBoundPropertywithValueType(valueType: Any.Type) -> PropertyProtocol<br class=""> {<br class=""> return Property<valueType>.init()<br class=""> }<br class="">}<br class=""></blockquote></div></body></html>