> Am 03.12.2017 um 18:36 schrieb Chris Lattner via swift-evolution 
> <swift-evolution@swift.org>:
> On Dec 2, 2017, at 7:11 PM, Matthew Johnson <matt...@anandabits.com 
> <mailto:matt...@anandabits.com>> wrote:
>>> This does not improve clarity of code, it merely serves to obfuscate logic. 
>>>  It is immediately apparent from the APIs being used, the API style, and 
>>> the static types (in Xcode or through static declarations) that this is all 
>>> Python stuff.  
>> It may be immediately apparent when the types involved are obviously 
>> dynamic, such as in this example where Python.import is explicitly used.  
>> However, my concern is less about the intended use case of dynamic language 
>> interop than I am that this feature will be generally available to all types 
>> in Swift.  
>> This is big change from AnyObject dispatch.  It opens up the dynamism to 
>> types and contexts that are not necessarily obviously using dynamic lookup, 
>> callable, etc.  Maybe this won’t turn out to be a problem in practice but I 
>> still think it’s a legitimate concern.
> Sure, it is a legit concern, but it is also nothing new.  This is the 
> standard concern with type inference.
> While there are weird cases, in practice, values do not get magicked out of 
> no-where.  They most commonly are either root values like:
>       let np = Python.import(“foo”)
>       let pyInt = PyVal(42)
> or they come for parameters:
>       func f(x : PyVal) {
> The place that is potentially surprising is when the type gets buried because 
> you’re working with some API that returns a [String, PyVal] dictionary or 
> something:
>       let x = foo()[“someKey”]
> and you don’t realize that PyVal’s are involved.  However, if you are 
> actively writing the code, you have access to code completion and other 
> things that tell you these types, and if it is important for the clarity of 
> the code, you write this instead:
>       let x :PyVal = foo()[“someKey”]
> There is nothing specific to this proposal about this issue.
>> I’m uncertain what the right answer is.  I’m still not really comfortable 
>> with opening up dynamic lookup to any user-defined type without some way to 
>> indicate to readers that dynamic lookup is happening in a piece of code.  
>> Maybe there is a less localized annotation that would indicate dynamic 
>> lookup is in effect for a larger chunk of code.  
> You seem to be extremely concerned that people will adopt DynamicMemberLookup 
> for types where it doesn’t make sense and abuse the feature.  I am having a 
> real problem understanding what your concern is, so I’d really love for you 
> to explain some theoretical examples of the bad thing that happens: why 
> someone (non-maliciously) adopts the protocol, what code gets written, and 
> what harm actually comes from it.
> Let me use a made up tale from a parallel universe to illustrate why I don’t 
> understand your concern.  Imagine if Swift didn’t already interoperate with 
> C, and did not already have IUOs.  Someone who cared about C language 
> interoperability would quickly realize that the ergonomics of importing 
> everything as strong optionals is a non-starter, jeopardizing the usability 
> of C interop, and would propose IUOs as a feature.
> We’d then have a long and drawn out conversation about the various options on 
> how to model this, the pros and cons of each, and would settle on IUO as the 
> least bad design (as an aside, in our universe, when we went through the 
> design process that led to IUOs, this is exactly what happened, we even 
> considered syntaxing them as interobangs :-).
> At that point, there would be a general uproar because IUOs have high 
> potential for abuse: Swift is “all about” strong types and safety, which IUOs 
> undermine.  Strong optionals are considered a pain to use by some people and 
> widely misunderstood (I think they are the biggest challenge in learning 
> Swift in practice), and so it is a reasonable feature that people could 
> pervasively adopt IUOs, leading to a much worse world all around.
> This made up parallel universe is exactly analogous to what is happening now. 
>  DynamicMemberLookup is no more dangerous and harmful than IUOs are.  

The difference is that IUOs are marked by a "!“ whereas dynamic member lookups 
look just like normal member lookups but unlike them can fail.
With the exception of corner cases like indexed lookups or arithmetic overflow 
Swift marks places where something can fail with "!“ or "Unsafe“ prefixes or 
"try“ or "?“. The latter examples are even protected from runtime failure 
(crashes) because the type system enforces handling the failing case. 
Nonetheless we put the "try“ and "?“ to mark those places (and with good 
One might even say that indexed lookups are marked by special syntax ("[…]“).

Now DynamicMemberLookup would introduce a new possibility where something can 
fail with a runtime crash and it is *not* visible that this can happen there. A 
typo in the member name is sufficient to trigger a crash (at least the 
PythonPlayground did crash when I changed a name being looked up - will this 
change? Probably not because there is nothing it could sensibly do!). 

This is not possible in current Swift and therefore it should be visible 


> They will be one more tool in the toolbox.  While it is possible that someone 
> will abuse it, this will not be widespread.  People who are particularly 
> worried will build a single new rule into their linters (which already flag 
> uses of x!), and the world will keep revolving.
>>> Even the behavior of AnyObject was carefully designed and considered, and 
>>> were really really good reasons for it returning IUO.
>> I am not trying to call into question the choices made in the past.  Swift 
>> wouldn’t be the great language with a bright future that it is today without 
>> an incredibly successful migration of a large user base from Objective-C to 
>> Swift.  This is a huge accomplishment and couldn’t have happened without 
>> making really good decisions about some really hard tradeoffs.
> You miss my point.  My point is that AnyObject lookup was carefully 
> considered, has stood the test of time, and is the *right* answer.  Swift 1 
> would not have been nearly as successful without it.
> -Chris
> _______________________________________________
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

swift-evolution mailing list

Reply via email to