<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="">Note that you can define a protocol which will allow your framework to instantiate the type, and to call methods on instances of that type. If you can structure your code in this fashion, it can be very elegant in that it doesn't require factory functions and it is &nbsp;type safe.<div class=""><br class=""></div><div class="">For example:</div><div class="">--</div><div class=""><div class="">struct GraphableDescription { }</div><div class=""><br class=""></div><div class="">protocol Graphable {</div><div class="">&nbsp; &nbsp; /// Construct a graphable item from a description.</div><div class="">&nbsp; &nbsp; init(description: GraphableDescription)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; func graph()</div><div class="">}</div><div class=""><br class=""></div><div class="">// Example framework method.</div><div class="">func graphItem(description: GraphableDescription, graphable: Graphable.Type) {</div><div class="">&nbsp; &nbsp; // Instantiate the graphable.</div><div class="">&nbsp; &nbsp; let item = graphable.init(description: description)</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; // Graph it.</div><div class="">&nbsp; &nbsp; item.graph()</div><div class="">}</div><div class=""><br class=""></div><div class="">// Example Graphable client.</div><div class="">struct Circle: Graphable {</div><div class="">&nbsp; &nbsp; init(description: GraphableDescription) { }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; func graph() { }</div><div class="">}</div><div class=""><br class=""></div><div class="">// Example framework client.</div><div class="">func foo() {</div><div class="">&nbsp; &nbsp; graphItem(GraphableDescription(), graphable: Circle.self)</div><div class="">}</div></div><div class="">--</div><div class=""><br class=""></div><div class=""><div class="">&nbsp;- Daniel</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 9:59 AM, Matthew Davies via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><div class="gmail_default">I don't really like the idea of a factory function, but unfortunately that might be the only way to do it :( However, due to my specific use case, I don't think a factory function will work. I'm working on a framework that will need to both instantiate the class from a string (or class type) <i class="">and</i>&nbsp;call methods dynamically on it. Which, I'm not sure I can do in the build tools that are provided in the open source package. Foundation hasn't been fully implemented and is missing a lot of the methods that would allow this to work.</div><div class="gmail_default"><br class=""></div><div class="gmail_default">@Jens thanks for that blog post. I'll have to make sure I check back to see what his solution is for it.</div><div class="gmail_default"><br class=""></div><div class=""><br class=""></div></div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class=""><div style="color:rgb(136,136,136);font-size:12.8000001907349px;max-width:600px;direction:ltr" class=""><div style="max-width:470px;margin:8px 8px 0px 0px" class=""><table border="0" cellspacing="0" cellpadding="0" width="470" style="width:470px" class=""><tbody class=""><tr valign="top" class=""><td style="font-family:Arial;font-stretch:normal;font-size:14px;color:rgb(100,100,100);padding-left:10px" class=""><div class=""><b class="">Matthew Davies</b><br class="">Junior Developer,&nbsp;<a href="http://geostrategies.com/" target="_blank" class="">GeoStrategies</a></div><div class="">Director of Photography, <a href="http://offblockfilms.com/" target="_blank" class="">OffBlock Films</a></div><div style="color:rgb(141,141,141);font-size:13px;padding:5px 0px" class=""><a href="tel:209-225.3246" style="color:rgb(141,141,141);text-decoration:none" target="_blank" class="">209-225-3246</a>&nbsp;<span style="color:rgb(102,102,102);display:inline-block" class="">|</span>&nbsp;<span style="display:inline-block" class=""><a href="tel:209-202-3284" style="color:rgb(141,141,141);text-decoration:none" target="_blank" class="">209-202-3284</a></span>&nbsp;<span style="color:rgb(102,102,102);display:inline-block" class="">|</span>&nbsp;<span style="display:inline-block" class=""><a href="mailto:daviesgeek@gmail.com" style="color:rgb(141,141,141);text-decoration:none" target="_blank" class="">daviesgeek@gmail.com</a></span>&nbsp;<span style="color:rgb(102,102,102);display:inline-block" class="">|</span>&nbsp;<span style="white-space:nowrap;display:inline-block" class=""><a href="https://daviesgeek.com/" style="color:rgb(141,141,141);text-decoration:none" target="_blank" class="">daviesgeek.com</a></span></div><div style="margin-top:5px" class=""><a href="http://facebook.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/facebook.png" width="16" height="16" class=""></a>&nbsp;<a href="http://us.linkedin.com/in/daviesgeek" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/linkedin.png" width="16" height="16" class=""></a>&nbsp;<a href="http://twitter.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/twitter.png" width="16" height="16" class=""></a>&nbsp;<a href="http://daviesgeek.com/feed.xml" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/blogRSS.png" width="16" height="16" class=""></a>&nbsp;&nbsp;<a href="http://github.com/daviesgeek" style="color:rgb(17,85,204)" target="_blank" class=""><img src="https://s3.amazonaws.com/images.wisestamp.com/icons/github.png" width="16" height="16" class=""></a></div></td></tr></tbody></table></div><br class=""></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br class=""><div class="gmail_quote">On Thu, Dec 10, 2015 at 9:30 AM, Jan Neumüller <span dir="ltr" class="">&lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">Please no factory madness in Swift. This stuff is bad enough in Java - don’t infect Swift with it.<div class=""><br class=""></div><div class="">Jan<br class="">
<br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On 10.12.2015, at 18:23, Jens Alfke via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""></div></div><div class=""><div style="word-wrap:break-word" class=""><div class=""><div class="h5"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 7:26 AM, Harlan Haskins via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""><div class=""><span style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important" class="">IIRC this isn’t possible because there’s no Runtime to query for classnames (it’s inherently unsafe anyway).</span></div></blockquote><div class=""><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div></div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">It’s not unsafe if you specify a base class/protocol that the loaded class must conform to.</div><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">You might want to look into a better way of doing that you’re trying to do.</div></div></blockquote></div><br class=""><div class="">I disagree with “a better way” — “a workaround” is how I’d rephrase it. This kind of dynamism is often the best tool for the job, and a lot of Cocoa developers are frustrated by its absence in Swift. For example, there’s a series of blog posts from earlier this year by the highly respected Brent Simmons [NetNewsWire, MarsEdit, Glassboard, etc., currently at Omni]:</div><div class=""><span style="white-space:pre-wrap" class="">        </span><a href="http://inessential.com/swiftdiary" target="_blank" class="">http://inessential.com/swiftdiary</a></div><div class=""><span style="white-space:pre-wrap" class="">        </span><a href="http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str" target="_blank" class="">http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str</a></div><div class=""><br class=""></div><div class="">The workaround I’d suggest is a factory function that contains a switch statement that matches class names and returns newly initialized instances.</div><div class=""><br class=""></div><div class="">—Jens</div>
</div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=LDGmuA3DoapHmeUK-2Bv6cUW-2BPLkX9OuRniq9VPkW9gLj8xJjFiYUqXP-2B-2BqeLw1DrrZfro7eAuKJLpWeIY6RXsT6ndRrfTk-2FASv6KTPeHvqYeHVgEehXehfZsR17lSKZwhNl2XSAelASCAkxIKLhm1CUFBrefO15pHpakZ-2Fl3gz-2FRRwk0slXwlmlQMlzYQi36iHyltwjO1-2B7bmLCIF6zh3dAn0vnoY5QLWwaU1XSykJQ0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
</div><span class="">
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br 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=""></span></div></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=eLFMrKDT8iBxZ-2Fbnk-2BZqvS8VFMZFE5zWEMX7mix5AIAkd3cIgkEMI23J6Zj58Nv4ENkYztTnnQxeopmIDZePfRVMD3G29FRSf7xu-2FEoK2qIUIm2ZQxKQEpVHUzh2TvtIev8TltG-2BH4zz4z7uQZcKhkYZORXIfwua1r6bjYj0l1tQt1XcGcMUeljsMHsy2WROtkT4NlS5SlmNPPsi1alCn7ZxCnUH9oKqX3JF7NFJ0J0-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" class="">
</div>
<br class="">_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=AdkfTiApI80cNEyortTzHbERtY5det-2FDBvSxuhs4q2PtyKQpyGCVmQsFQNbWeUdHppZ-2FflR60OwKBKNtU-2B7hU-2F5q7FNOB-2BrTdopT8flSnpdJ-2FHEDC9-2BXJgpzOTitnFOcAWcppZwjIwFd3Z3tMTGsl1Tv-2FiK6VBZ-2BMRoLnFk8KPshCSZZsk7mfU4JxB7V6xOFEUkLQBbBXRIoAvR2ZNusHAB2jONH9kh13gcdWT-2Bhtq0-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<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=""></div></div></body></html>