Swift hidden options

Night tweets recap. Swift compilation time is very slow sometimes. Especially when you’re going to use
map, filter, reduce, … and compiler must find out types & friends. It drives me
crazy and I’m forced to replace almost all my map calls with for ... in loops.

I simply do not want to wait 14s to compile simple stuff like this one:

let images: [NSImage] = {
guard let bundle = NSBundle(identifier: "com.topmonks.apps.Monk") else {
return []
}

let images = (0...3).map({ bundle.URLForResource("logo($0)", withExtension: "png") })
.filter({ $0 != nil })
.map({ NSImage(contentsOfURL: $0!) })
.filter({ $0 != nil })
.map({ $0! })

return images
}()

Silly example, but 14s, common? And here comes the Joe Pamer’s tweet – response to Rob Rix
about Swift compiler profiling. Open your project build settings and add …

-Xfrontend -debug-time-function-bodies

… to Other Swift Flags in Swift Compiler – Custom Flags section.

Build your project and inspect build log.

Now you have an idea what part of code causes slow compilation time. Use it in rdar
and temporarily modify your code until all reported issues will be resolved. The best way to handle
it now.

I did post it to Twitter (with typo, it’s not -Xfront,
but it should be -Xfrontend) and Marcin did ask
where I get this one. I replied that from Joe Pamer’s tweet
and then realized what if there are more flags like this one.

Here comes my favorite Hopper Disassembler, which helps me to find -help-hidden
flag. I tried it and here’re the outputs:

Interesting flags there. For example -enable-throw-without-try, which allows you to compile following code …

enum MyError: ErrorType {
case Whatever
}

func doSomething() throws {
throw MyError.Whatever
}

doSomething()

… without Call can throw but is not marked with ‘try’ error. Maybe this one will make it to the final
Xcode 7 & Swift 2? Who knows, better not. Code is shorter, yes, but I like it (despite my first reaction,
didn’t like it). More verbose, but it says here it can throw, which makes bug hunting, … easier.

It also reveals some features they’re working on, like:

  • -enable-experimental-patterns (Enable experimental ‘switch’ pattern matching features),
  • -enable-experimental-unavailable-as-optional (Enable experimental treat unavailable symbols as optional).

And many more. Didn’t have time to inspect them all. Let me know if you find something interesting.

These flags are not hidden just for fun. Remember, they’re experimental, to test features they’re working
on, to help them with debugging, etc.

Don’t use them in production code! Just play with them.