Different behavior of Optional in Swift 4 and Swift 4.1

Hi, Everyone,

I have two questions about the Optional in swift 4.1, here is the code for the first one.

let a: String! = "hello, world"

let c: Any = a

print(c)

in Swift 4,

the above code result is "hello, world"

but in Swift 4.1,

the above code result is some("hello, world")

does swift 4.1 change something for the Optional ?

And the second.

let a: String! = "hello, world"

let c: Any = a

print(c)

switch c {

case Optional.some(let value):

print(value)

case Optional.none: // Case is already handled by previous patterns; consider removing it

print(c)

}

When I try to use switch/case for Optional as the code above, I have the warning message,

**Case is already handled by previous patterns; consider removing it. **

but the second case is obviously not handled by the previous patterns, is this normal, or am I miss something.

Best Regards.

I think the changes you’re seeing are because of this proposal.

2 Likes

For the first case, take a look at this Q&A:

Essentially, the printing of IUOs changed in Swift 4.1 with the removal of Custom(Debug)StringConvertible conformance from ImplicitlyUnwrappedOptional to make progress towards removing the type itself. In Swift 4.2 however, the ImplicitlyUnwrappedOptional type is gone and you'll be dealing with proper Optional values instead, which will print like Optional("hello world").

The second case is the bug SR-6975, but it has already been fixed by #15264, and will be fixed in Swift 4.2.