<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 30, 2016, at 9:40 AM, Jay Abbott &lt;<a href="mailto:jay@abbott.me.uk" class="">jay@abbott.me.uk</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">Robert,<br class=""><br class=""></div>What it does is allow developers to extend the language to do things that it doesn't support. Associated Objects is just a flexible way to allow developers to do that, and that's how I used it in Objective-C, so that's what I thought of in Swift when I found that "I want to do &lt;x&gt; but the language doesn't support it". Maybe there's a completely different way to achieve the same thing - but I just went with what I know.<br class=""></div></div></div></div></div></div></div></div></blockquote><div><br class=""></div><div>"I want to do &lt;x&gt; but the language doesn't support it” is the whole reason for this list! &nbsp;I want to know what “&lt;x&gt;” is in order to wrap my head around your proposal better. &nbsp;I’m not here to invalidate your work with a slew of criticisms, I’m just trying to understand your proposal by probing you about it.</div><br class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">So it's difficult to come up with concrete examples of "things a developer might want to do but the language doesn't support" because almost by definition they are unforeseen. I can only enumerate the things I have wanted to do in Obj-C and Swift and how I got around it.<br class=""></div>- Dynamically add properties to a class (obc-c) / implement stored properties (swift).<br class=""></div>- Add per-instance methods at run-time.<br class=""></div>- Perform a function when some other object is deallocated (haven't solved this in Swift yet, but in obj-c associated object deallocation is well-defined so I used that).<br class=""></div>- Other unforeseen things…</div></div></div></div></blockquote><div><br class=""></div><div>Again, these are features and I want motivations behind them. &nbsp;<i class="">Why</i> should you be able to dynamically add stored properties and methods to <i class="">any</i>&nbsp;Swift class? &nbsp;<i class="">Why</i> should you be able to observe deallocation if deallocation calls are not guaranteed (remember, Objective-C and Swift do not guarantee -dealloc/deinit calls in any order at any time - please don’t assume an RAII-like model because it will lead to memory leaks and malformed code).</div><div><br class=""></div><div>I will say: Perhaps if you’re having trouble motivating the inclusion of this feature, you may want to step back and reevaluate its place in the language and why you wanted to write this library in the first place. &nbsp;Often times, I find that really helps get the creative juices flowing. &nbsp;Or, if I can’t come up with anything, it means I go back to step 1 and start over. &nbsp;Proposals can be as much a learning process for us as it is for you.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><br class=""></div>So maybe Associated Objects isn't the answer and I should have stated the problem better, instead of jumping to what I thought the answer might be... the problem I want to solve is this:<br class=""><br class=""></div>As a developer I want to do &lt;x&gt; but the language doesn't support it... what helpful thing *can* I use right now that allows me to achieve this? I accept the disclaimer by ticking this box:<br class="">[ ] Yes, I understand that I'm not using Swift anymore, but some custom run-time thing that I'm building myself on top of Swift, so if I want type safety I have to implement it, if I want copy-on-write or other optimisations, I have to implement it, and I understand that performance might not be the best too.<br class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><br class=""></div></div></div></div></div></div></div></div></div></blockquote><div><br class=""></div><div>So from what I’ve gathered you’ve written a library to do this yourself. &nbsp;You have a vision for how you want to use this library. &nbsp;You think that vision is compatible with a language-level change. &nbsp;From my perspective, your job now is to articulate that vision and motivate it to this list. &nbsp;I don’t think associated objects and the other dynamic features you mention are fundamentally incompatible with Swift, I just think the design that I’ve seen so far may not be the best way of going about it and I’m voicing my concerns as much. &nbsp;</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class=""><div class="">If Swift can provide something to help developers go beyond the abilities the current version, isn't that a good idea?<br class=""></div></div></div></div></div></div></div></div></div></blockquote><div><br class=""></div><div>No one disputes this. &nbsp;We just want to see your rationale.</div><br class=""><blockquote type="cite" class=""><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Fri, 30 Sep 2016 at 07:13 Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">&gt; On Sep 28, 2016, at 9:27 AM, Robert Widmann via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; To make this kind of pattern type safe you would, for example, need to keep track metatype pointers too and use the dynamic cast machinery to check the type on retrieval.<br class="">
<br class="">
Actually, a somewhat different (DispatchSpecificKey-style) design makes type safety pretty easy:<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; import Cocoa<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; let myValue = AssociatedValue(.strong, ofType: String.self, on: NSView.self)<br class="">
<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; let view = NSView()<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; myValue[view] = "Hello, world!"<br class="">
&nbsp; &nbsp; &nbsp; &nbsp; myValue[view]<br class="">
<br class="">
Implementation here: &lt;<a href="https://gist.github.com/brentdax/75bfd619379fea53d8ca8afaa16d95bb" rel="noreferrer" target="_blank" class="">https://gist.github.com/brentdax/75bfd619379fea53d8ca8afaa16d95bb</a>&gt;<br class="">
<br class="">
Nevertheless, I don't think this should be shipped in Swift; associated objects are as esoteric as they come.<br class="">
<br class="">
--<br class="">
Brent Royal-Gordon<br class="">
Architechies<br class="">
<br class="">
</blockquote></div>
</div></blockquote></div><br class=""></body></html>