Swift 1.2 improves the “let” keyword, and other improvements

Apple released Swift 1.2 today as part of the Xcode 6.3 beta. There are lots of big changes, but one that I particularly like:

let constants are now more powerful and consistent — The new rule is that a let constant must be initialized before use (like a var), and that it may only be initialized, not reassigned or mutated after initialization.

I’ve been trying Swift for about two months on a couple of academic exercises (like the Matasano Challenges and Project Euler). I was planning on writing up some of my thoughts, and the behaviour of the let keyword was going to be one of my complaints, but now that seems to have been fixed. Hooray!

In Swift 1.1, variables supported an “initialise now, assign later” model, whereas constants had to be assigned at the point of initialisation:

var x: String   // This is okay
x = "foo"

let y: String   // This is a compiler error
y = "bar"

The new changes mean that constants also support “initialise now, assign later”, and the above is legal code. The old behaviour never broke anything, but it was a mild annoyance that I hit pretty regularly. The change won’t radically change the way I use Swift, but it will make my code a little smoother.

I’m impressed with the way Swift is improving and evolving. I don’t think it’s ready to be used for production code, because it’s still changing in pretty significant ways, but it seems to be for the better. I’m looking forward to seeing what the first “stable” version of Swift looks like.