<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 &lt;<a href="mailto:joanna@carterconsulting.org.uk" class="">joanna@carterconsulting.org.uk</a>&gt; 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) -&gt; Any? {<br class=""> if let deserializableType = type as? Deserializable.Type {<br class=""> &nbsp;&nbsp;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=""> &nbsp;var untypedValue: Any? { get }<br class="">}<br class=""><br class="">public struct Property&lt;PropertyType : Any&gt;<br class="">{<br class=""> &nbsp;public let info: PropertyInfo<br class=""><br class=""> &nbsp;public var name: String<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;return info.name<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;public var displayName: String<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;return info.displayName<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;public var value: PropertyType?<br class=""><br class=""> &nbsp;public init()<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;self.init(propertyInfo: PropertyInfo(), value: nil)<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;init(propertyInfo: PropertyInfo, value: PropertyType?)<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;self.value = value<br class=""><br class=""> &nbsp;&nbsp;&nbsp;<a href="http://self.info" class="">self.info</a> = propertyInfo;<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;init(propertyInfo: PropertyInfo)<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;self.init(propertyInfo: propertyInfo, value: nil)<br class=""> &nbsp;}<br class=""><br class=""> &nbsp;init(other: Property&lt;PropertyType&gt;)<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;self.init(propertyInfo: <a href="http://other.info" class="">other.info</a>, value: other.value)<br class=""> &nbsp;}<br class="">}<br class=""><br class="">struct PropertyFactory<br class="">{<br class=""> &nbsp;static func createBoundPropertywithValueType(valueType: Any.Type) -&gt; PropertyProtocol<br class=""> &nbsp;{<br class=""> &nbsp;&nbsp;&nbsp;return Property&lt;valueType&gt;.init()<br class=""> &nbsp;}<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&lt;T: PropertyType&gt;: 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="">&nbsp; private static func create() -&gt; Property&lt;Self&gt; {</div><div class="">&nbsp; &nbsp; return Property&lt;Self&gt;.init()</div><div class="">&nbsp; }</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="">&nbsp; static func createBoundPropertywithValueType(valueType: PropertyType.Type) -&gt; PropertyProtocol</div><div class="">&nbsp; {<br class="">&nbsp; &nbsp; return valueType.create()<br class="">&nbsp; }</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="">&nbsp;static func createBoundPropertywithValueType(valueType: Any.Type) -&gt; PropertyProtocol<br class="">&nbsp;{<br class="">&nbsp;&nbsp;&nbsp;return Property&lt;valueType&gt;.init()<br class="">&nbsp;}<br class="">}<br class=""></blockquote></div></body></html>