4 August 2014 4min read

Thoughts after knowing Swift

In these weeks, since Apple has announced his new programming language, I've been trying to learn something about it. As I said before, my approach hasn't been a full change on mind, I think that is to early for that. Instead, I tried to read a lot of posts, follow the changes and write some code. Now that I finished reading the Swift book I know all the basics about the new language.

With that knowledge I think that I can say some things about Swift: things that I like and things that I will miss.

Named parameters πŸ‘

First of all, I love that they maintained the verbosity of Objective-C with the named parameters. This was my major fear. I really love the Smalltalk aproach to messaging and naming parameters, I think that makes my code readable without any comment. The other cool thing about the Swift parameters is the diferent behaviours that they have: constants, variables, inout, default values. This is a thing that I really like.

Constant until proven variable πŸ‘

Talking about constants and variables, I really love the introduction of the 'let' keyword to define constants in our code. This difference from C make our code more reliable and secure. I think that now the best approach is to write all our code with constants until you need to modify something. This makes you more concient about the code that you are writing.

Not private by default πŸ‘Ž

When creating new classes I always make everything private by default. Then I start moving things to the public interface thinking carefully about the API that I want to expose. With this I make sure to think carefully about the behaviour of my class. If later I need to acces some private thing I need to think about it twice.

With Siwft and its accessor system all is internal by default. It's better than public by default but I don't like it. I would prefer that everything was private and force me to write public or internal in the parts that I want to expose. With this default behaviour they are making a difference between classes in the same framework and outside classes, and for me there is not a big difference. You also have to think about your internal API.

No default implementations πŸ‘Ž

I'm really disappointed in seeing that there is no way to define default implementations for protocols. I was expecting that with this new language we finally would have a way to do multiple inheritance, something like traits.

The worse part is that the syntax for defining inheritance and protocol conformance is the same, so the first time I thought that we could inherit from different classes. Nope.

class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {
    // class definition goes here
}

I think that traits is a very powerful and useful feature. I always disliked inheritance, it's good in some cases but normally you end up having high coupling that will be difficult to remove.

With traits you can separate that functionality in little classes and make your class adopt the ones that it need without implementing anything. I really like protocols but I will love to see how the we will use traits.

No instance properties in extensions πŸ‘Ž

Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

This is a really bad notice for me. I'm sure that there is some good reason behind that but I don't like it. We can use computed properties but there is no way to save things in an extension. We will need to continue relying in associated objects?

*I just hope that the reason behind that is not to be compatible with Objective-C or something like this. I don't want to see the new language be harmed by the old one. It will be like the limitations of Objecite-C by being a superset of C. *

Powerful types πŸ‘

I really love of all the types on Swift are equally powerful. The enums and structs can have almost all the things that a class can have, this is really good.

No pointers, ARC for the win πŸ‘

ARC demonstrated to be a reliable technology for memory management. Now that we don't have pointers all is managed by the compiler so it wil be better for us.

The good part is that even without having pointers we still have some things from a low level language: value types vs. reference types, inout parameters, etc. This makes Swift a hight level but powerful language.

It's just a beta

Of course there is a lot more cool (and maybe bad) things in Swift. But here I just wanted to point some of the things that surprised me. For me the major disappointments are traits and instantes properties on extensions. But other than that I really like the language. I didn't talk about closures, curried functions, generic programming... you can see that there are a lot of things. And all of this will be better in a future.

Remember, this is still a beta.

If you enjoyed this post

Continue reading