<div dir="ltr"><div>Reading this part of the proposal<br>"Now clients are able to write more natural code like:
<code>json[0]?.name?.first?.stringValue</code> which is close to the expressivity of Javascript...
while being fully type safe!"<br><br></div>gave me the feeling that communication between dynamiclookupable types and static swift types was aimed at being quite seamless. <br>Now having to write ".string or .int" in the end of the dynamic expression would indeed raise the bar a bit, but i'm still very skeptical this would be enough to deter people from trying to import "dynamic magic" into swift. <br><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 7, 2017 at 6:20 PM, BJ Homer <span dir="ltr"><<a href="mailto:bjhomer@gmail.com" target="_blank">bjhomer@gmail.com</a>></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"><div><div class="h5"><br><div><br><blockquote type="cite"><div>On Dec 7, 2017, at 10:16 AM, Benjamin G <<a href="mailto:benjamin.garrigues@gmail.com" target="_blank">benjamin.garrigues@gmail.com</a>> wrote:</div><br class="m_-6383463788070718005Apple-interchange-newline"><div><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"><<a href="mailto:bjhomer@gmail.com" target="_blank">bjhomer@gmail.com</a>></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><div>On Dec 7, 2017, at 7:11 AM, Benjamin G via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div></span><div><span><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'm saying is simply impossible because of Swift type system even with the new proposal, i'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> MyDynamicObject: </span>DynamicMemberLookupProtocol<span> {</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><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255);min-height:13px"> <br class="m_-6383463788070718005m_-2249381074317712002webkit-block-placeholder"></div><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>) -> <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> x: </span>MyDynamicObject<span> = ...</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> > <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="m_-6383463788070718005HOEnZb"><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'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>
</div></blockquote></div><div><br></div></div></div><div>As far as I am aware, the proposal provides no mechanism for automatically converting back from PyVal to regular Swift types. You would have to do something explicit like <font face="Menlo">myPythonValue.intValue</font>, or <font face="Menlo">String(myPythonValue)</font>.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-BJ</div><div><br></div></font></span></div></blockquote></div><br></div>