<div dir="ltr"><div><div><div><div><div><div>I have implemented Associated Objects (and values) in pure swift here:<br><a href="https://github.com/j-h-a/AssociatedObjects">https://github.com/j-h-a/AssociatedObjects</a><br><br></div>The README is very short and straight-forward so I won&#39;t re-describe it here.<br><br></div>The implementation isn&#39;t great, but it&#39;s a small and simple proof of concept. I have further extended this (not in GH, but very simple and happy to share if anyone cares) to add dynamic methods using closures onto individual object instances. Useful for user interactions, or adding &#39;actions&#39; to objects.<br><br></div>I&#39;d like to propose that this or something similar be added to the standard library. It could potentially even be added to AnyObject so that developers can use it without having to declare an extension for whichever classes they want to use it on.<br><br></div>As mentioned, this can easily be extended (either in the standard library or by developers) to add closures dynamically to any object, or to any class, which could serve as a useful way for those not concerned with type safety and the like to get some dynamic behaviour in there in the shorter term. With a little language support it could even be used to implement stored properties in extensions very easily.<br><br></div>A better implementation would need some language changes - specifically deinit hooks (has that ever been discussed?) because of the way weak references are lazily zeroed. Another implementation improvement might lazily add the dictionary to each object instead of storing it globally - not sure if this would have ABI implications.<br><br></div>Interested in feedback and thoughts :)<br></div>