<html><head></head><body><div style="color:#000; background-color:#fff; font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px">Unsubscribe<div id="yui_3_16_0_ym19_1_1500037587519_90427"><span></span></div><div class="qtdSeparateBR" id="yui_3_16_0_ym19_1_1500037587519_90426"><br><br></div><div class="yahoo_quoted" id="yui_3_16_0_ym19_1_1500037587519_90418" style="display: block;">  <div style="font-family: Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_ym19_1_1500037587519_90417"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_ym19_1_1500037587519_90416"> <div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90415"> <font id="yui_3_16_0_ym19_1_1500037587519_90424" size="2" face="Arial"> <hr size="1"> <b><span style="font-weight:bold;">From:</span></b> "swift-users-request@swift.org" &lt;swift-users-request@swift.org&gt;<br> <b><span style="font-weight: bold;">To:</span></b> swift-users@swift.org <br> <b><span style="font-weight: bold;">Sent:</span></b> Friday, July 14, 2017 12:57 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> swift-users Digest, Vol 20, Issue 13<br> </font> </div> <div class="y_msg_container" id="yui_3_16_0_ym19_1_1500037587519_90454"><br><div dir="ltr">Send swift-users mailing list submissions to<br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; <a ymailto="mailto:swift-users@swift.org" href="mailto:swift-users@swift.org">swift-users@swift.org</a><br></div><div dir="ltr"><br></div><div dir="ltr">To subscribe or unsubscribe via the World Wide Web, visit<br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; <a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90458">or, via email, send a message with subject or body 'help' to<br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90456">&nbsp;&nbsp;&nbsp; <a ymailto="mailto:swift-users-request@swift.org" href="mailto:swift-users-request@swift.org">swift-users-request@swift.org</a><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90453"><br></div><div dir="ltr">You can reach the person managing the list at<br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90468">&nbsp;&nbsp;&nbsp; <a ymailto="mailto:swift-users-owner@swift.org" href="mailto:swift-users-owner@swift.org" id="yui_3_16_0_ym19_1_1500037587519_90467">swift-users-owner@swift.org</a><br></div><div dir="ltr"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90470">When replying, please edit your Subject line so it is more specific<br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90472">than "Re: Contents of swift-users digest..."<br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90474"><br></div><div dir="ltr" id="yui_3_16_0_ym19_1_1500037587519_90476"><br></div><div dir="ltr">Today's Topics:<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;  1. Swift 4 emulating Decoder behaviour (Joanna Carter)<br></div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr">----------------------------------------------------------------------<br></div><div dir="ltr"><br></div><div dir="ltr">Message: 1<br></div><div dir="ltr">Date: Thu, 13 Jul 2017 20:46:20 +0200<br></div><div dir="ltr">From: Joanna Carter &lt;<a ymailto="mailto:joanna@carterconsulting.org.uk" href="mailto:joanna@carterconsulting.org.uk">joanna@carterconsulting.org.uk</a>&gt;<br></div><div dir="ltr">To: <a ymailto="mailto:swift-users@swift.org" href="mailto:swift-users@swift.org">swift-users@swift.org</a><br></div><div dir="ltr">Subject: [swift-users] Swift 4 emulating Decoder behaviour<br></div><div dir="ltr">Message-ID:<br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; &lt;<a ymailto="mailto:A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk" href="mailto:A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk">A8DE64E2-5360-4B35-87D2-69F3F5B562E7@carterconsulting.org.uk</a>&gt;<br></div><div dir="ltr">Content-Type: text/plain; charset=utf-8<br></div><div dir="ltr"><br></div><div dir="ltr">Greetings<br></div><div dir="ltr"><br></div><div dir="ltr">I notice that, with Swift 4, I can decode an object like this :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • let retrievedSpot = try decoder.decode(ParkingSpot.self, from: retrievedData)&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">And that will return a ParkingSpot, as expected, into retrievedSpot.<br></div><div dir="ltr"><br></div><div dir="ltr">However, I thought I would try the same technique with one of my pet projects…<br></div><div dir="ltr"><br></div><div dir="ltr">I have a protocol&nbsp; and an implementing struct :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • public protocol PropertyProtocol&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  static var propertyType: Any.Type { get }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  var untypedValue: Any? { get }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • public struct Property&lt;valueT : DefaultValueProvider&gt; : PropertyProtocol&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  public static var propertyType: Any.Type&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  return valueT.self&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  public var untypedValue: Any?&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  return value&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  public var value = valueT()&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • }&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">Now, I want to be able to use a "factory" method to create an instance of Property&lt;T&gt;, bound to its parameter type. So, I followed the same principal as Decoder :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • struct PropertyFactory&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  static func createProperty&lt;T&gt;(_ type: T.Type) -&gt; PropertyProtocol where T : DefaultValueProvider&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  return type.createProperty()&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • }&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">DefaultValueProvider is defined as follows and String is extended to conform to it :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • public protocol DefaultValueProvider&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  init()&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • }&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • extension String : DefaultValueProvider { }&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">Now, this works fine if I pass a known type :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; • let pproperty = PropertyFactory.createProperty(String.self)&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">But, if I hold the type to be passed in in an Any.Type or DefaultValueProvider.Type variable, then doing this :<br></div><div dir="ltr"><br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  let type: Any.Type = String.self&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp;  <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  if let propertyType = type as? DefaultValueProvider.Type&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp;  {&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp; &nbsp;  let p = PropertyFactory.createProperty(propertyType)&nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp; &nbsp; <br></div><div dir="ltr">&nbsp;&nbsp;&nbsp; •&nbsp; &nbsp; &nbsp;  …&nbsp; <br></div><div dir="ltr"><br></div><div dir="ltr">Fails to compile with the message : Cannot invoke 'createProperty' with an argument list of type '(DefaultValueProvider.Type)'<br></div><div dir="ltr"><br></div><div dir="ltr">Likewise Decoder.decode(…) will not accept storing the type in an Any.Type or Decodable.Type variable.<br></div><div dir="ltr"><br></div><div dir="ltr">I find this odd and perplexing. Is this a known issue or has nobody yet realised that this could be useful ?<br></div><div dir="ltr"><br></div><div dir="ltr">Joanna<br></div><div dir="ltr"><br></div><div dir="ltr">--<br></div><div dir="ltr">Joanna Carter<br></div><div dir="ltr">Carter Consulting<br></div><div dir="ltr"><br></div><div dir="ltr">------------------------------<br></div><div dir="ltr"><br></div><div dir="ltr">_______________________________________________<br></div><div dir="ltr">swift-users mailing list<br></div><div dir="ltr"><a ymailto="mailto:swift-users@swift.org" href="mailto:swift-users@swift.org">swift-users@swift.org</a><br></div><div dir="ltr"><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr">End of swift-users Digest, Vol 20, Issue 13<br></div><div dir="ltr">*******************************************<br></div><br><br></div> </div> </div>  </div></div></body></html>