Is Swift dynamic enough?

Twitt-fight about Swift

I become a part of another Twitt-fight about Swift. This time it was about lack of dynamic features in the language. So I promised to write a blog post to clear up my position.

In the beginning I would like to discuss what makes a language like ObjC dynamic.

Let’s start with a feature set:

  1. Reflection and Introspection of an object and a class (http://stackoverflow.com/questions/2299841/objective-c-introspection-reflection)
  2. Dynamic object creation (http://stackoverflow.com/questions/2175547/objective-c-dynamic-object-creation)
  3. Dynamic class creation (https://www.mikeash.com/pyblog/friday-qa-2010-11-19-creating-classes-at-runtime-for-fun-and-profit.html)
  4. Key Value Coding and Observing (http://www.andyibanez.com/key-value-coding-key-value-observing-cocoa-bindings-basic-tutorial/)
  5. Method swizzling (http://nshipster.com/method-swizzling/)
  6. Method forwarding (https://www.mikeash.com/pyblog/friday-qa-2009-03-27-objective-c-message-forwarding.html)

When we speak about programming languages we tend to generalize. We tend to speak of programming language but mean the whole ecosystem, which contains:

  • development tools
  • runtime
  • standard libraries
  • and programming language

It is important to mention that development tools and runtime in Apples ecosystem are currently hosting 4.2 programming languages:

  • C
  • C++
  • ObjC/ObjC++
  • Swift

Keeping this in mind I would like to note that, most of the dynamic features listed perviously are featured by the runtime and standard libraries not the programming language itself. The only features of ObjC language, relevant for the dynamic concepts are @selector() and @protocol() keywords. All the runtime functions we use for the features (1, 2, 5) are implemented in C, and features (3, 4, 6) are possible because of the implementation of NSObject class.

So what we are missing in Swift is not language features. We miss implementation of functions in standard library. There are a few on the horizon (https://gist.github.com/mchambers/fb9da554898dae3e54f2 http://stackoverflow.com/questions/24049673/swift-class-introspection-generics) but it is just the beginning. Btw. if you use objc keyword when defining Swift classes everything works.


Now here is for the crazy ones, if you want to go completely dynamic just abandom classes all together. Dictionaries and functions are enough when you switch your mindset to dynamic programming. This is basically how programming languages like JavaScript, Clojure and Erlang work.


Last words of confidence. Even if we don’t get dynamic features in the final version of Swift standard libraries. There is nothing wrong with using ObjC. The eco system was extended by another language, it doesn’t mean that ObjC will vanish any time soon. I am sure that Apple will keep there frameworks ObjC compatible for a long period. The main adoption drivers will be third party frameworks in Swift. However this won’t be critical for anyones business any time soon.