<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" <swift-users-request@swift.org><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"> <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"> <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"> <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"> <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"> 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 <<a ymailto="mailto:joanna@carterconsulting.org.uk" href="mailto:joanna@carterconsulting.org.uk">joanna@carterconsulting.org.uk</a>><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"> <<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>><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"> • let retrievedSpot = try decoder.decode(ParkingSpot.self, from: retrievedData) <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 and an implementing struct :<br></div><div dir="ltr"><br></div><div dir="ltr"> • public protocol PropertyProtocol <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • static var propertyType: Any.Type { get } <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • var untypedValue: Any? { get } <br></div><div dir="ltr"> • } <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • public struct Property<valueT : DefaultValueProvider> : PropertyProtocol <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • public static var propertyType: Any.Type <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • return valueT.self <br></div><div dir="ltr"> • } <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • public var untypedValue: Any? <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • return value <br></div><div dir="ltr"> • } <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • public var value = valueT() <br></div><div dir="ltr"> • } <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<T>, bound to its parameter type. So, I followed the same principal as Decoder :<br></div><div dir="ltr"><br></div><div dir="ltr"> • struct PropertyFactory <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • static func createProperty<T>(_ type: T.Type) -> PropertyProtocol where T : DefaultValueProvider <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • return type.createProperty() <br></div><div dir="ltr"> • } <br></div><div dir="ltr"> • } <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"> • public protocol DefaultValueProvider <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • init() <br></div><div dir="ltr"> • } <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • extension String : DefaultValueProvider { } <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"> • let pproperty = PropertyFactory.createProperty(String.self) <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"> • let type: Any.Type = String.self <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • if let propertyType = type as? DefaultValueProvider.Type <br></div><div dir="ltr"> • { <br></div><div dir="ltr"> • let p = PropertyFactory.createProperty(propertyType) <br></div><div dir="ltr"> • <br></div><div dir="ltr"> • … <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>