<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 7, 2017 at 6:01 PM, BJ Homer <span dir="ltr">&lt;<a href="mailto:bjhomer@gmail.com" target="_blank">bjhomer@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">Benjamin, <div><br></div><div>It sounds like your concern is that people might write objects that just store everything in a dictionary, instead of declaring typed properties. Correct?<br><div><br><blockquote type="cite"><span class=""><div>On Dec 7, 2017, at 7:11 AM, Benjamin G via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div></span><div><span class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_extra"><div class="gmail_quote"><div><br></div>I think i answered that question many times as well (by masquerading dictionaries as regular classes), but if what i&#39;m saying is simply impossible because of Swift type system even with the new proposal, i&#39;d rather people tell it to me than ask the same question over and over..</div><br></div></div></span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps: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">______________________________<wbr>_________________</span></div></blockquote><br></div><div>It <i>would</i> be possible, in theory, to write a class that used a dictionary as a backing store for all its properties under this proposal. However, the dictionary would have to be typed <font face="Menlo">[String: Any?]</font>, and every property access would be typed <font face="Menlo">Any?</font>. Your code would look like this:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(112,61,170);background-color:rgb(255,255,255)"><span style="color:rgb(186,45,162)">class</span><span style="color:rgb(0,0,0)"> MyDynamicObject: </span>DynamicMemberLookupProtocol<span style="color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    <span style="color:#ba2da2">var</span> storage: [<span style="color:#703daa">String</span>: <span style="color:#ba2da2">Any</span>?] = [:]</div><p style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px">    <br class="m_-2249381074317712002webkit-block-placeholder"></p><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    <span style="color:#ba2da2">subscript</span>(dynamicMember: <span style="color:#703daa">String</span>) -&gt; <span style="color:#ba2da2">Any</span>? {</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        <span style="color:#ba2da2">get</span> { <span style="color:#ba2da2">return</span> <span style="color:#4f8187">storage</span>[dynamicMember] }</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">        <span style="color:#ba2da2">set</span>(newValue) { <span style="color:#4f8187">storage</span>[dynamicMember] = newValue }</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    }</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(79,129,135);background-color:rgb(255,255,255)"><span style="color:#ba2da2">let</span><span style="color:#000000"> x: </span>MyDynamicObject<span style="color:#000000"> = ...</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)">// setting properties is easy!</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><a href="http://x.name" target="_blank">x.name</a> = “Benjamin”</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">x.age = <font color="#272ad8">3</font></div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(0,132,0);background-color:rgb(255,255,255)">// using them, though, is hard!</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">var</span> adults: [<span style="color:#703daa">String</span>] = []</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="color:#ba2da2">if</span> x.age <span style="color:#ba2da2">as</span>! <span style="color:#703daa">Int</span> &gt; <span style="color:#272ad8">18</span> {</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">    <span style="color:#4f8187">adults</span>.append(<a href="http://x.name" target="_blank">x.name</a> <span style="color:#ba2da2">as</span>! <span style="color:#703daa">String</span>)</div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)">}</div></div></blockquote><div><br></div><div>If you decide to create an object that stores everything in an ‘<font face="Menlo">Any</font>’, Swift is going to force you to use an ‘<font face="Menlo">as</font>’ cast anytime you want to use it. That’s super annoying.</div><div><br></div><div>So yes, it’s possible to create a type that masquerades a dictionary as a regular class. But the ergonomics of doing so are sufficiently frustrating that nobody is likely to do so. Swift makes it easy to declare typed variables here, and the user experience for doing so is vastly improved (code completion, compile-time checking, no more ‘<font face="Menlo">as</font>’ casting, etc).</div><div><br></div><div>So while it’s theoretically possible to do this, I don’t think it’s a concern in practice. The incentives are already in place to encourage doing the “right” thing in this case, even with the possibility of dynamic member lookup.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-BJ</div></font></span></div></div></blockquote><div><br></div><div>Hey, thanks for answering that part of the concern. i also came to this point, but wondered if the dictionary couldn&#39;t be made of type [String: DynamicValue] where DynamicValue would have some way to be  automatically casted to string, int, etc, by reusing the same mechanism that the proposal would use to convert PyVal to regular swift types. I stopped at this point.<br></div></div><br></div></div>