5 ms·
Method dispatch in Swift
- dep_b 10y agoThey did a great job of almost plastering over all the typical Objective-C and Cocoa things in Swift but you can't escape some of these weird underlying mechanisms of Objective-C creeping in without sacrificing compatibility. Great read, good to know this stuff. I encountered a few of these examples while poking around with more complex features in the language.
- melling 10y agoMakes you wonder how long before Apple stops making the compromises in compatibility. In the near term, sure, but 3-5 years down the road, they may favor Swift.
- deleted 10y ago[deleted]
- johncolanduoni 10y agoIt would definitely be a good place to reuse their Cocoa/Carbon strategy.
- pjmlp 10y agoThey already do. All the recent Objective-C features were added to improve the compatibility between both languages, reducing Objective-Cisms in Swift.
- dep_b 10y agoI noticed that nullable nonnull did almost nothing for my Objective-C code. A real bummer, I would love to have a good analyzer warning for that.
- KingOfBrian 10y agoI'm not sure what they're sacrificing in terms of compatibility. Do you have any examples? There is a degree of complexity, but providing interoperability with most languages more complex than C will introduce complexity.
- the_duke 10y agoNow I just wish that you get get Swift in Arch... The AUR packages are broken without downgrading llvm which I can't do atm, and there are no prebuilt generic linux versions... -.-
- drewcrawford 10y agoThe problem with Arch is that Swift relies quite heavily on Glibc so the work to port to alternate libc is very substantial. The other problem is that the mess basically starts there, e.g. Swift programs in practice also depend on Glibc and so after you kill the first turtle it's still turtles all the way down. You could build an Arch with Glibc, but why you would want to is not immediately clear.
- mwcampbell 10y agoI think you're confusing Arch with Alpine. Alpine is based on musl; Arch is based on glibc.
- qwertyuiop924 10y ago...? Arch is glibc-based. What are you on about?
- CalChris 10y agoJava uses table dispatch by default, but you can opt into direct dispatch by using the final keyword. C++ uses direct dispatch by default, but you can opt into table dispatch by adding the virtual keyword. Objective-C always uses message dispatch, but allows developers to fall back to C in order to get the performance gains of direct dispatch. Swift has taken on the noble goal of supporting all three types of dispatch. I kinda already knew this but it's nice and succinct.
- corysama 10y agoFor the under-the-hood details of message dispatch, see "An Illustrated History of objc_msgSend" http://sealiesoftware.com/msg/index.html http://sealiesoftware.com/msg/index.html
- jdmichal 10y agoJust for completeness, I believe C# fits with C++, which is something that can occasionally catch a Java developer.
- pjmlp 10y agoC# is actually a mix. It uses static dispatch by default, virtual dispatch like C++ and Object Pascal, dynamic dispatch like Objective-C.
- noblethrasher 10y agoInterestingly, while the C# language uses static dispatch by default, it is at least the case that Microsoft's compiler always emits the `callvirt` instruction[1]. This is because the language spec requires that a method call to a null instance of a reference type must throw a NullReferenceException, and callvirt is apparently the cheapest way to do that on the CLR. [1] https://blogs.msdn.microsoft.com/ericgu/2008/07/02/why-does-c-always-use-callvirt/ https://blogs.msdn.microsoft.com/ericgu/2008/07/02/why-does-... [2] §6.1 of the C# language spec.
- KingOfBrian 10y ago
- twoodfin 10y agoCan anyone explain the etymology of "witness table"?
- johncolanduoni 10y agoThe only analogy I can think of is to a "witness" to a logical proposition in mathematics. In this case the witnesses "prove" that the given concrete type implements the given abstract type. If that's the case it's kind of a weird way to put it, though it sort of squares with the witnesses in proof-relevant formulations of logout where you care about the form of a proof and not just what it proves.
- KingOfBrian 10y agoGood question, I'll update the post if I can figure it out. It turns out that swift uses both witness and virtual tables, something I'm realizing I didn't make very clear in the post. https://github.com/apple/swift/blob/master/docs/SIL.rst#vtables https://github.com/apple/swift/blob/master/docs/SIL.rst#vtab... has some more interesting information.
- Someone 10y agoI agree with johncolanduoni that it likely derives from https://en.wikipedia.org/wiki/Witness_(mathematics) https://en.wikipedia.org/wiki/Witness_(mathematics): "a witness is a specific value t to be substituted for variable x of an existential statement of the form ∃x φ(x) such that φ(t) is true" In the less abstract: a witness is someone who, because he has looked at data, can tell you something about that data. A witness table contains the statements of multiple witnesses. In this case, the statements are of the form "'A' is a value for which the statement 'you can find the code implementing method M for class C at address x' is true". As the article states "Most languages refer to this as a “virtual table". I find the use of Witness table strange here. I know it from string matching algorithms, where the witness statements are of forms such as "if you start comparing here, the fourth character is the first that doesn't match".
- duaneb 10y agoI must say, while swift can be very readable, just reading through this article put me off of the language. If you don't work with objective-c or uikit/cocoa, is there a reason to use this over another llvm-based language with cleaner, simpler semantics? The two bugs outlined are terrifying.
- fredsir 10y agoWhich other llvm-based language to you recommend? Swift is, to me, elegant and intuitive to use, quick to write, and supports a lot of nice features which you don't find in say C++, and supports stuff that, say, Rust lacks support for. `Optional`s being baked into the language, using `Result` types, `guards`, `if let`, pattern matching, great support for Rx, and lots of other fun and cool stuff, easy use of high-level features with possibility to dive deeper, while still being able to use all the features of Cocoa, Cocoa Touch, and all the other frameworks and features of macOS, iOS, watchOS, and appletvOS as a native citizen, and also write code that runs better everyday on Linux, is a really great package. Swift Package Manager is also great and becomes better everyday. Swift is a good combination of a great language that supports a lot of cool platforms and has a lot of tools available.
- falsedan 10y agoFunny, the description of message-passing dispatch is the same as Perl's & Python method resolution, including the caching of recently-resolved methods for fast direct dispatch…
- falsedan 10y agoFunny, the description of message-passing dispatch is the same as Perl's & Python method resolution, including the caching of recently-resolved methods for fast direct dispatch…