<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=""><div class="">There was a proposal for this a little while ago, what you’re looking for are mixins, though I’m not sure they would solve your problem either.</div><div class=""><br class=""></div><div class="">The reason this can’t be done as part of an extension is that there are only really two ways to implement it:</div><div class=""><br class=""></div><div class=""><b class="">Expand the Type</b>: Basically the new data has to be added to the type somehow. For example, a struct of four Ints is about 32 bytes, to add a stored property you’d need to increase that. This could be fine if it’s internal to a module, as it can be computed in advance, but if you’re extending an imported type (as in your case) then this isn’t ideal as your version of UITextFields would be a different from the size expected by other libraries that don’t know about your extension, which would mean that some kind of transformation is required between your version of UITextFields and any other variation of it that you need to work with; not impossible, but not the nicest thing to have to do and potentially not great for performance.</div><div class=""><br class=""></div><div class=""><b class="">Lookup Added Properties</b>: I believe this is exactly what you’re doing with your workaround; namely your added properties are being stored separately and associated to an instance so that they can be looked up/changed without touching the size of the original. However, this kind of lookup isn’t great for performance either.</div><div class=""><br class=""></div><div class="">So both options would have to trade performance for stored property style syntax, which IMO would not be a great thing to do as it would hide what’s going on behind the scenes to make it work. Maybe if we had some kind of Swiftier methods for doing either option explicitly that might be okay, since it could give simpler means that explicitly expose the performance impact, but APIs designed to handle type-erased wrappers shouldn’t need anything like this. Of course that’s no comfort when dealing with APIs that do, especially legacy ones, but you’ve found a workaround that seems to do what you need, and there may be other options (can’t think of any just now).</div><div class=""><br class=""></div><div class="">Anyway, that’s the gist of the problem as I understand it, hope some of that helps to fill in why it’s not a straightforward thing to address.</div><br class=""><div><blockquote type="cite" class=""><div class="">On 4 Apr 2016, at 11:00, Yogev Sitton via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@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="">Hi,<div class=""><br class=""></div><div class="">I try to avoid using inheritance for just adding functionality - for that I use (and love) extensions.</div><div class="">This works great for functions - but I find the lack for stored properties support limiting.</div><div class=""><br class=""></div><div class="">Here’s my use case:</div><div class="">I want to map every textfield in my view controller to a JSON field - so it would be very helpful to add a string <i class="">Key</i> property to all of the UITextFields in my app.</div><div class="">For now this is how I solve this issue (inside the extension):</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; line-height: normal;" class=""><font face="HelveticaNeue" class=""><i class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> customProperties {</span></i></font></div></div><i class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">static</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">var</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> key : </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">String</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">?</span><br class=""><span style="font-family: HelveticaNeue;" class="">}</span><br class=""><span class="Apple-tab-span" style="font-family: HelveticaNeue; white-space: pre;">        </span><br class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">var</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> key : </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">String</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">? {</span></i><div class=""><i class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>get</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> {</span><br class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">return</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">objc_getAssociatedObject</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">, &amp;</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">customProperties</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">.key) </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">as</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">? </span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">String</span><br class=""><span class="Apple-tab-span" style="font-family: HelveticaNeue; white-space: pre;">        </span><span style="font-family: HelveticaNeue;" class="">}</span><br class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">        </span></span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">set</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""> (value){</span><br class=""><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space:pre">                </span></span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">objc_setAssociatedObject</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">self</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">,&amp;</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">customProperties</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">.key,value,.</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">OBJC_ASSOCIATION_RETAIN_NONATOMIC</span><span style="font-family: HelveticaNeue; font-variant-ligatures: no-common-ligatures;" class="">)</span><br class=""><span class="Apple-tab-span" style="font-family: HelveticaNeue; white-space: pre;">        </span><span style="font-family: HelveticaNeue;" class="">}</span><br class=""></i><div class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><font face="HelveticaNeue" class=""><i class="">}</i></font></span></div></div></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><font face="HelveticaNeue" class=""><i class=""><br class=""></i></font></span></div><div style="margin: 0px; line-height: normal;" class=""><font face="HelveticaNeue" class="">I would love to just add an actual variable to the extension without the need to use Obj-C associated objects.</font></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>