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