Any and Any? in Swift are weird. We venture forth into the depth of unknown using Swift Reflection API as the basis for our research and find out some inconsistent behaviors for Swift casting API, when working with Any.

Recently, when playing around with Swift Reflection API, I stumbled upon a painful problem. Basically, when typecasting Any and Any? to protocols Swift just couldn’t help it and started screwing me in all my glory.

Without the further ado, here is the code, that ate me, raped me and killed what was left (in that specific order, mind you):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 protocol Greetable { } class Nested : Greetable { } class Container { let nested = Nested ( ) let optionalNested : Nested ? = Nested ( ) } struct Keys { static let nested = "nested" static let optionalNested = "optionalNested" } func value ( forKey key : String ? ) -> Any ? { return Mirror ( reflecting : Container ( ) ) . children . filter { $ 0 . label == key } . first ? . value } func cast < T , R > ( _ x : T ) -> R ? { return x as ? R } func printCasts < T > ( forKey key : String , withType type : T . Type ) { let variable = value ( forKey : key ) print ( "----------------------" ) print ( "Type = " + variable . debugDescription ) if ( variable as ? T ) != nil { print ( " \ ( type ) " ) } if ( variable as ? Greetable ) != nil { print ( " \ ( type ) Greetable" ) } if ( variable as ? AnyObject as ? Greetable ) != nil { print ( " \ ( type ) Greetable as AnyObject" ) } }

You can read more about the cast function in my previous Type Inference in Swift blog post.

If we run the code for nested property, the results would be as expected:

1 2 printCasts ( forKey : Keys . nested , withType : Nested . self )

1 2 3 4 5 Type = Optional ( Nested ) Nested Nested Greetable Nested Greetable as AnyObject

The fun part starts, when we try to run the printCasts with optionalNested property:

1 2 printCasts ( forKey : Keys . optionalNested , withType : Nested . self )

The results of the call are as weird as T-Rex trying to put my hat on while hiding in my wardrobe:

1 2 3 4 Type = Optional ( Optional ( Nested ) ) Nested Nested Greetable as AnyObject

Swift managed to unwrap the Optional<Optional<Nested>> into nested, failed to do so, when unwrapping to Greetable protocol, to which Nested conforms, but managed to do the double optional unwrapping to protocol, when we’ve done an intermediate cast to AnyObject.

That is really bad at the most basic level, as we can’t use the same function for handling different values hidden by Any and we don’t have any non explicit way of knowing, what’s in there.

Please note, that the irregular behavior in here is the unwrapping of double optional. Value for optionalNested key is optional by itself, as the property is optional, and it is wrapped in another optional, when first? is called. For optionalNested none of the three conditions should have worked. This point is valid for explicit unwrapping from Type?? to Type, where Type != Any:

1 2 3 4 5 let integer : Int ? ? = 1 if let casted = integer as ? Int { print ( casted ) }

This results in compiler error: downcast from 'Int??' to 'Int' only unwraps optionals.

I tried playing around and found another interesting behavior:

1 2 3 4 5 let integer : Int ? ? = 1 if let casted = integer as ? Any ? as ? Int { print ( casted ) }

Prints 1 . So, it seems, like the compiler automatically unwraps, when casting to Any? and then if let casted to Type results in another unwrap.

So, how should we get around that?

We could try doing the cast to Optional<Optional<Greetable>> :

1 2 3 4 if ( variable as ? Greetable ? ? ) != nil { print ( " \ ( type ) Optional Greetable" ) }

Sadly, that won’t do, as the compiler wouldn’t even consider compiling the thing. It would just return an error instead: error: cannot downcast from 'Any?' to a more optional type 'Greetable??'

That creeped me out, as Any could be of any type, meaning, that it could be an Optional, other wrapper (e.g. Either, Result, etc.) under the hood. Seems, like Any is just not defined well enough at that point in space and time (just imagine how lucky someone is in a parallel universe, where swiftc is well-written, doesn’t lag or crash).

Casting to AnyObject or Any is a hack and we should of course avoid that, as we could never be sure as to why it actually works and if future compiler versions would break it.

A better(???) way would be to override the compiler checks and cast to appropriate types. We could do that by using the generic casting function:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 func cast < T , R > ( _ x : T ) -> R ? { return x as ? R } func betterPrintCasts < T > ( forKey key : String , withType type : T . Type ) { let reflected = value ( forKey : key ) print ( "----------------------" ) print ( "Type = " + reflected . debugDescription ) if let casted : T = cast ( reflected ) { print ( " \ ( type ( of : casted ) )" ) } if let casted : Greetable ? = cast ( reflected ) { print ( " \ ( type ( of : casted ) ) Greetable?" ) } if let casted : Greetable = cast ( reflected ) { print ( " \ ( type ( of : casted ) ) Greetable" ) } } betterPrintCasts ( forKey : Keys . optionalNested , withType : Nested . self )

This yields us better results in terms of types, but Nested is still being unwrapped from double optionals.

1 2 3 4 Type = Optional ( Optional ( Nested ) ) Nested Optional < Greetable > Greetable ?

But the best solution is to actually avoid Any? at all costs and be as explicit, as possible. Build your code around generics instead. On the other hand, there are not that much Swift APIs return the result typed as Any, which could be both Type and Optional<Type> under the hood. So, if you use them, think in terms of proper language behavior (even, if this results in bloating your code). Don’t forget to cover everything Any-related with tests. I mean it, 100% test coverage of your code, library code, kid(s), wife(s)/husband(s), dog(s), cat(s), hamster(s), house(s), car(s), parent(s) and anything else you treasure is a must, as you never know, when the compiler would change it’s behavior. And don’t even try saying I didn’t warn you.

For my case, I just ignore the type system from the Swift Reflection API (namely, Mirror) based on Any and pretend I’m using ordinary possible values from my properties. Moreover, I explicitly unwrap the Optional<Any> to Any before casting just in case. This lets me avoid implicit double optional unwrapping and feel myself safe and secure (although I’m unsure this actually helps, as betterPrintCasts works the same way):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 func goodPrintCasts < T > ( forKey key : String , withType type : T . Type ) { print ( "-----------Good-----------" ) value ( forKey : key ) . map { reflected in if let casted : T = cast ( reflected ) { print ( " \ ( type ( of : casted ) )" ) } if let casted : Greetable ? = cast ( reflected ) { print ( " \ ( type ( of : casted ) ) Greetable?" ) } } } goodPrintCasts ( forKey : Keys . nested , withType : Nested . self ) goodPrintCasts ( forKey : Keys . optionalNested , withType : Nested . self )

results in the following output for optionalNested and nested property:

1 2 3 Nested Optional < Greetable > Greetable ?

That’s all, folks. Have a great day and stay DRY, no matter, where you are.