Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

Why are implicitly unwrapped optionals not unwrapped when using string interpolation in Swift 3?

Example: Running the following code in the playground

var str: String! str = "Hello"  print("The following should not be printed as an optional: \(str)") 

produces this output:

The following should not be printed as an optional: Optional("Hello") 

Of course I can concatenate strings with the + operator but I'm using string interpolation pretty much everywhere in my app which now doesn't work anymore due to this (bug?).

Is this even a bug or did they intentionally change this behaviour with Swift 3?

like image 521
Keiwan Avatar asked Sep 16 '16 17:09

Keiwan


Video Answer


1 Answers

As per SE-0054, ImplicitlyUnwrappedOptional<T> is no longer a distinct type; there is only Optional<T> now.

Declarations are still allowed to be annotated as implicitly unwrapped optionals T!, but doing so just adds a hidden attribute to inform the compiler that their value may be force unwrapped in contexts that demand their unwrapped type T; their actual type is now T?.

So you can think of this declaration:

var str: String! 

as actually looking like this:

@_implicitlyUnwrapped // this attribute name is fictitious  var str: String? 

Only the compiler sees this @_implicitlyUnwrapped attribute, but what it allows for is the implicit unwrapping of str's value in contexts that demand a String (its unwrapped type):

// `str` cannot be type-checked as a strong optional, so the compiler will // implicitly force unwrap it (causing a crash in this case) let x: String = str  // We're accessing a member on the unwrapped type of `str`, so it'll also be // implicitly force unwrapped here print(str.count) 

But in all other cases where str can be type-checked as a strong optional, it will be:

// `x` is inferred to be a `String?` (because we really are assigning a `String?`) let x = str   let y: Any = str // `str` is implicitly coerced from `String?` to `Any`  print(str) // Same as the previous example, as `print` takes an `Any` parameter. 

And the compiler will always prefer treating it as such over force unwrapping.

As the proposal says (emphasis mine):

If the expression can be explicitly type checked with a strong optional type, it will be. However, the type checker will fall back to forcing the optional if necessary. The effect of this behavior is that the result of any expression that refers to a value declared as T! will either have type T or type T?.

When it comes to string interpolation, under the hood the compiler uses this initialiser from the _ExpressibleByStringInterpolation protocol in order to evaluate a string interpolation segment:

/// Creates an instance containing the appropriate representation for the /// given value. /// /// Do not call this initializer directly. It is used by the compiler for /// each string interpolation segment when you use string interpolation. For /// example: /// ///     let s = "\(5) x \(2) = \(5 * 2)" ///     print(s) ///     // Prints "5 x 2 = 10" /// /// This initializer is called five times when processing the string literal /// in the example above; once each for the following: the integer `5`, the /// string `" x "`, the integer `2`, the string `" = "`, and the result of /// the expression `5 * 2`. /// /// - Parameter expr: The expression to represent. init<T>(stringInterpolationSegment expr: T) 

Therefore when implicitly called by your code:

var str: String! str = "Hello"  print("The following should not be printed as an optional: \(str)") 

As str's actual type is String?, by default that's what the compiler will infer the generic placeholder T to be. Therefore the value of str won't be force unwrapped, and you'll end up seeing the description for an optional.

If you wish for an IUO to be force unwrapped when used in string interpolation, you can simply use the force unwrap operator !:

var str: String! str = "Hello"  print("The following should not be printed as an optional: \(str!)") 

or you can coerce to its non-optional type (in this case String) in order to force the compiler to implicitly force unwrap it for you:

print("The following should not be printed as an optional: \(str as String)") 

both of which, of course, will crash if str is nil.

like image 56
Hamish Avatar answered Oct 02 '22 06:10

Hamish