Earlier quoted context omitted.
The Objective-C old guard has had a very hard time dealing with Swift. For a couple years now, every 3 months they have another blog post hand-wringing about what they used to do in Objective-C, how they can't in Swift, with the implicit warning this is worrisome and _needs_ to be reimplemented in Swift, or Swift isn't a serious contender long-term. Most of the of the community seems to have moved on, with the unders…
I do hope Apple unveils a unified protocol-oriented framework for both MacOS and iOS. One that, when necessary, keeps Swift in mind before Objective-C.
Dynamic Swift
11–20 of 54 posts
Re: Dynamic Swift
#12 if localObject.foo != serverObject.foo {
localObject.foo = serverObject.foo
changeDictionary[fooKey] = serverObject.foo
}
if localObject.bar != serverObject.bar {
localObject.foo = serverObject.bar
changeDictionary[barKey] = serverObject.bar
}
But there's another language feature that can replace boilerplate, without dynamism and its associated performance and type safety penalties. A feature that's reviled by many, and was omitted from Swift, but serves this purpose perfectly: macros. What if you could write this? macro_rules! merge {
($localObject:expr, $serverObject:expr, ($($prop:ident),+)) => {
$(
if $localObject.$prop != $serverObject.$prop {
$localObject.$prop = $serverObject.$prop;
changeDictionary[stringify!($prop)] = $serverObject.$prop;
}
)+
}
}
merge!(localObject, serverObject, (foo, bar, baz));
Actually, this is valid Rust code, as you might have guessed if you know that language. I picked it because it has similar syntax to Swift and a powerful, hygienic macro system. (This case is simple enough that a C macro would also work fine without any ugly arcane stuff - you'd have to have a separate macro invocation for each property, but whatever. But Rust's system scales better to slightly more complicated scenarios.)Admittedly, not everything you can implement with dynamism is as easy to replace with macros. Many things are essentially impossible with the simple pattern matching language used above. In Rust, there is also the option to write a compiler plugin that can do arbitrary-ish things with ASTs, so almost anything is possible with enough work, including things you can't implement with dynamism, but fiddling with AST transformations is hairy enough that boilerplate looks a lot more attractive, relatively speaking.
Even so, I bet macros would satisfy most of the people advocating for "Dynamic Swift". And for the people who hate them - do you really think doing crazy things at runtime is any safer or easier to understand? It's certainly slower.
(By the way, you can implement dynamism with macros. For example, you could autogenerate functions like 'setProperty(name: String, val: Any)' that switch on the name and do the appropriate casting and such. Though depending on your attitude, you might consider this crucial enough that it should be built into the language.)
Re: Dynamic Swift
#13> Leaning on the Objective-C runtime feels like a temporary solution because it only exists on the Mac and iOS. That's not entirely true; libobjc2[0] exists, for example, and is fully compatible with objc4. [0]: https://github.com/gnustep/libobjc2
Re: Dynamic Swift
#14Not sure I see what's going on here (it's just a blog post linking to other blog posts?), but is this arguing that Apple should turn the safety of Swift back into the unsafe mess of Objective-C? Horrifying stuff like swizzling, respondsToSelector tricks, etc.? That's what Swift is trying to get away from! Some of the classes in Apple's frameworks need to be overridden, some of them cannot be overridden, some of them…
When there is widespread discussion of a certain topic in the Mac/iOS programming community, Michael Tsai is well known for posting collections of links to important discussion of that topic, as he has done here.
These are mostly grizzled Objectrive-C veterans writing. They have dealt with the messy legacy for years, but they also leverage the dynamism of Obj-C/Cocoa to solve a ton of real problems.
Personally, I really like Swift. After 15 years of Objective-C programming, I probably won't do more than few more hours of it over the remainder of my life. But, I understand where these guys are coming from, and it's interesting to read about the problems they are having.
This totally isn't a "language war", but if you look at it that way, it is one of the most fascinating ones.
It's not just comparing and contrasting two programming languages arbitrarily, in theory; it's an energetic discussion among accomplished working programmers about real-world work in the only two languages that can feasibly be used for the platforms they are on. Super interesting.
Re: Dynamic Swift
#15Not sure I see what's going on here (it's just a blog post linking to other blog posts?), but is this arguing that Apple should turn the safety of Swift back into the unsafe mess of Objective-C? Horrifying stuff like swizzling, respondsToSelector tricks, etc.? That's what Swift is trying to get away from! Some of the classes in Apple's frameworks need to be overridden, some of them cannot be overridden, some of them…
The Objective-C old guard has had a very hard time dealing with Swift. For a couple years now, every 3 months they have another blog post hand-wringing about what they used to do in Objective-C, how they can't in Swift, with the implicit warning this is worrisome and _needs_ to be reimplemented in Swift, or Swift isn't a serious contender long-term. Most of the of the community seems to have moved on, with the unders…
Me, too. At work, I am on two different projects at the moment: an SDK and application written in Swift, and a run-of-the-mill Rails app.
Going back to the Rails app is frankly gut-wrenchingly bad. Every aspect of coding and debugging — and especially refactoring — is so much worse when doing it in ruby.
The strength of Swift type system makes the tools so much better, and at least for me, it's easier to code in it and reason about it effectively (even if yes, you do type more letters).
But, I want to point out: the same is not true of Objective-C. Even though it has a lot of dynamism under the covers, coding in Obj-C in Xcode is much closer to coding in Swift than Ruby. It's only in 'special cases' that you would really use an untyped object reference. So the compiler is able to catch a huge number of errors at compile time, the editor knows what type your variable is so autocomplete is intelligent, etc.
I do like Swift better, but coding in Obj-C is much closer to Swift than Ruby in terms of the tooling experience, and in terms of how much dynamic untyped code there actually is in a program. Modern Obj-C is "mostly typed", and swizzling and weird object casting are seldomly done (usually only in a tricky situation, which is when you appreciate the ability).
Re: Dynamic Swift
#16Swift is not a perfect language, there's a lot of low hanging fruit (some of which is being dealt with in Swift 3), but it's lack of support for dynamic behavior is not one of those problems.
Re: Dynamic Swift
#17"Great web frameworks like Ruby on Rails, for example, can’t be built without relying on a more dynamic language." You don't necessarily want dynamism, you want metaprogramming.
Interesting, could you provide some examples?
Re: Dynamic Swift
#18Not sure I see what's going on here (it's just a blog post linking to other blog posts?), but is this arguing that Apple should turn the safety of Swift back into the unsafe mess of Objective-C? Horrifying stuff like swizzling, respondsToSelector tricks, etc.? That's what Swift is trying to get away from! Some of the classes in Apple's frameworks need to be overridden, some of them cannot be overridden, some of them…
Quotes:
"many of these dynamic features are definitely hacks, with various issues"
or "[Objective-C] is a bad design, as far as metasystems are concerned"
or "The solution to bad metasystems is not to ban metasystems, it is to design better metasystems that allow these things in a more disciplined and more flexible way."
or "The point is that these problems will need solving in a possible future world without the Objective-C runtime. These kinds of problems should be considered as that world is being designed. The answers don’t have to be the same answers as Objective-C — but they need to be good answers, better than (for instance) writing a script to generate some code."Re: Dynamic Swift
#19The posts in the first linked series ( http://inessential.com/2016/05/ ) seem to mostly focus on how dynamism can avoid the need for boilerplate. For example, he states that the following code is tedious to write and error-prone: if localObject.foo != serverObject.foo { localObject.foo = serverObject.foo changeDictionary[fooKey] = serverObject.foo } if localObject.bar != serverObject.bar { localObject.foo = serverObj…
That's the least of problems, since nowadays we have smart runtime systems that dynamically optimize running programs. It's not elegant, but it works. A more fundamental problem is that this approach to programming leads to brittle code, and when it breaks, it doesn't warn you until it's too late.
Re: Dynamic Swift
#20Relevant reply by Chris Lattner on the Swift Evolution mailing list: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mo...
What these posts talk about is the expressiveness of a metasystem, and they are generally agnostic about whether this metasystem is static or dynamic, just that it should not be less expressive than the current dynamic one.