[swift-evolution] Proposal: Introduce User-defined "Dynamic Member Lookup" Types

BJ Homer bjhomer at gmail.com
Fri Dec 1 10:59:21 CST 2017


Responses inline.

> On Dec 1, 2017, at 8:35 AM, Jon Gilbert via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> On Nov 30, 2017, at 08:10, Karl Wagner via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Personally, I feel this system is designed to let you write Python, using Swift as a wrapper language - except unlike with Objective-C,
> 
> Let me paraphrase the proposal—it basically says this system is designed to make it easier to “fake import” existing Python/Ruby/JS libs to make it easier for people who depend on them to adopt Swift. Especially in the world of server-side Swift, I can see how this could help speed Swift’s adoption.
> 
> However, I believe that you bring up an extremely valid concern. To extrapolate from your points, this proposal seems to have a high potential to erode what makes Swift special, and it provides a way to get around Swift’s safe-guards, avoid writing Swift versions of libraries, and ultimately, avoid hiring real Swift developers. 
> 

The point is not to let you write Python in Swift. If you want to write Python, just write Python. The point is to let you call Python libraries (or any dynamic language) from Swift, in the same way that we can already call C libraries from Swift. The ability to call C libraries does not make people “avoid hiring real Swift developers”; it just means that Swift developers can take advantage of C libraries. The goal of this proposal is to make Swift able to use more existing libraries. It sounds like your argument is “we want people to rewrite those libraries in Swift instead”. That’s a great goal, but it will only be achieved if Swift gains the kind of widespread cross-platform adoption that would be facilitated by this very proposal.


>> it doesn’t have any static information about what you can do or not. It can’t provide the “safe by default” guarantees are other core principles and set Swift apart from other languages.
> 
> Exactly. 
> 
> Look how JSON parsing is done in Swift 3.2 and 4.0—we make static types. The 1’s and 0’s from a web response go through layers of proper error handling before they become proper types Swift.
> 
> So why does this proposal keeps mentioning JSON parsing as an excuse for making a dynamic ghetto within the language? Because some people are parsing untyped JSON? Well, the solution for that is, type your frickin’ JSON. 

It doesn’t. There was one brief mention of a JSON example, to show how this proposal is useful for more than just Python interop. But most of the proposal talks about Python, not JSON.

> 
> Why compromise Swift’s static-ness and roll out the red carpet for the dynamic, unwashed masses?
> 
> Wouldn’t it be better to create an AI that quickly translates Pyton, Ruby, or JS into Swift? Augustus built huge public bathhouses for the unwashed Romans. 

If you can create an AI that quickly and accurately translates Python, Ruby, and JS into Swift, go right ahead. To the extent that those projects are not taking advantage of the dynamic features of their language, that’s likely feasible. It’s going to be a lot more difficult for the kind of code that does take advantage of the dynamic nature of the language. For example, dynamic languages can inspect the function name to vary its behavior. You can write one “fetchAll____FromDatabase()” function, and then call it as fetchAllEntiresFromDatabase(), fetchAllUsersFromDatabase(), or fetchAllCommentsFromDatabase(), and automatically get the right behavior. I don’t think you’re going to be able to automatically create an AI that mimics that behavior on Swift; it fundamentally relies on the dynamic binding present in those languages.

It’s clear that you think dynamic languages are inferior to static languages. That’s fine, but the fact is that there are existing useful libraries in those languages which do not currently exist in Swift. Is it your argument that Swift should not be able to make use of such libraries because of their “unclean-ness”? I disagree.

> 
>> When we consider interoperability with other languages, it should be from the perspective of mapping their features to Swift’s philosophies.
> 
> Amen.
> 
>> This proposal takes the reverse approach, and makes Swift like Python, so I’m against it.
> 
> This is my concern as well. 
> 
> Perhaps there is a way to make the proposal require that you declare Swift types and protocols that map to these other languages, with verbose error handling for when this glue breaks down (just like we have for typed JSON). 
> 
> Then, we could also make scripts that auto-generate the necessary Swift typing boilerplate interface code from the Python/Ruby/JS source, kinda like we get for Obj.-C bridging headers. This could be a good step towards the full AI translation that I mentioned.
> 
> Of course, maybe I am missing something here, and my opinion is wrong; Chris will let us know if these concerns are valid.
> 
> However, I hope we don’t hear how we are just being paranoid, because I assure you, this is not paranoia. It’s just about maintaining the static-ness and purity of Swift to help save the world from bad code.

You seem to conflate “libraries written in a dynamic language” with “bad code”. That’s an unjustified assumption that ignores the large quantity of existing very useful libraries written in those languages. Or maybe you’re assuming that if this proposal goes through, people will choose to write dynamic code in their native Swift projects? It’s certainly possible that some people will do that, but the way this proposal is written makes it unlikely for dynamic behavior to spread throughout the project itself, because you still need an underlying source of dynamism. This proposal only makes it possible to call into an existing dynamic system; it does not introduce a new dynamic system directly into Swift.

> 
> Everything in life and engineering is a trade-off. Swift’s trademark is that it consistently sacrifices the convenience of dynamic-ness for the safety and performance of static-ness. This proposal does seem to do the opposite, and we’d be naive not to fear the effect this will have. 
> 
> Ask yourself... what if Product knows about it? The last conversation a tech lead or engineering manager wants to have, I would imagine, is to explain on deaf ears why it would be bad to cut costs by farming out the iOS work to a cheap Python shop overseas, then just wrap it in sugary compiler hooks. Or why it would be bad for a Swift project to depend upon a stack of third-language dependencies that CocoaPods/Carthage can’t manage, XCode can’t analyze, stack traces can’t delve into (or can it?), lldb can’t step through, etc.
> 
> I do not believe this is unreasonable paranoia, I just believe it looks that way to people who work with an elite squad of engineers at a top company, who have perhaps not seen what goes on at most places, where budgets are tight and lots of people are not the biggest fans of Apple. It’s already with much grinding of teeth that non-Windows, non-Linux devs are necessary.

If someone’s wanting to farm out their iOS app to a 3rd party, they can already do that, whether or not Swift supports calling into dynamic libraries. They can even already do it from Swift with the existing C API for calling Python, in fact; this proposal just makes it a little easier. But if the entire app is really written in Python, a few C shims to get into Python-land aren’t a large burden. And they can already do it via ObjC as well. I think it’s unlikely that writing an entire iOS app in Python would be an efficient way to do things, but if that’s actually a better way to write the project, why are we stopping them? Is it the goal of the core team to encourage people to use Swift even when it’s not the best choice? I would suggest rather that the goal should be to improve Swift to make it the best choice. If that means being able to call into some existing business logic written in dyanmic_language_here, why is that problematic? If an engineering manager cannot make a convincing case that writing their app in Swift or ObjC is a better idea, then perhaps there’s not much of a case to be made. I find this scenario implausible, though, for a variety of reasons. For one, Python code can’t take advantage of the various system frameworks like Swift or ObjC can.

> 
> Given Swift’s emphasis on safety, this push to make danger easier really does surprise me, unless I just don’t understand this correctly. Have we re-evaluated the importance of having the language itself prohibit bad practices? Or is there some reason why this proposal does not actually make it easier to write code that looks like good, type-safe Swift code, but actually isn’t?
> 
> Finally, to really be honest, may I ask why web devs, many of whom never touched a line of Swift, deserve all this dynamic sugar, while on the other hand, we SWIFT devs don’t get any? How will people feel who were told that dynamic is so unsafe that Swift was designed to prevent it altogether? People who have been through all the migrations and member-wise trenches might chafe a bit. Is that unreasonable?

It seems odd that you talk about “deserving” dynamic sugar. The goal of this proposal is to make it easier to use more existing libraries, via a little bit of sugar goes a lot way toward that goal. All Swift users can take advantage of that. If we can make Swift a more viable language for web development (by enabling them to use existing dynamic libraries) without harming the language, why shouldn’t we? Of course, I think your argument is that this proposal does harm the language, but I fail to see how that’s the case. This proposal does precisely nothing to the language unless you’re calling methods on a type that conforms to DynamicMemberLookupProtocol. And when you’re doing that, the dynamism is well contained; it only exists those types. None of your other code is affected in any way.

> 
> Lastly, if you can “fake import” Javascript or Python to Swift via this proposal, is there anything that would prevent the “fake import” of masquerading Swift modules? In other words, could lazy devs who want hacky dynamic-ness abuse this? If so then maybe I’m in favor after all ;P (Just kidding.)

You can’t “fake import” Swift modules using this, because there’s no good way to look up Swift classes or methods by name, so you would have no way to make it work. This is about leveraging existing dynamic systems, not introducing new ones.

> 
> - Jon
> 
> PS—I am just trying to think critically here. Ultimately this proposal would not hurt me, but I am just worried about the big picture. I can see why it’s a tough call and I am sure that some pressures exist that might make even this compromise to be worth it. I just feel like we should not underestimate how much bad will come along with the good, nor should we assume, “Well that kind of bad would never happen where I work, and only idiots would abuse this.”



-BJ

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171201/14e52b9f/attachment-0001.html>


More information about the swift-evolution mailing list