<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="">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><blockquote type="cite" class=""><div class="">On 10.12.2015, at 18:23, Jens Alfke 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=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><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" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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 class="" style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div></div><div class="" style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">It’s not unsafe if you specify a base class/protocol that the loaded class must conform to.</div><div class="" style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><blockquote type="cite" class=""><div class=""><div class="" style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">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 class="Apple-tab-span" style="white-space:pre">        </span><a href="http://inessential.com/swiftdiary" class="">http://inessential.com/swiftdiary</a></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span><a href="http://inessential.com/2015/07/20/swift_diary_1_class_or_struct_from_str" 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>
<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="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="">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></body></html>