2

Here is the screenshot of my Xcode Playground:

Screenshot of Playground

As you can see, the str is printed as some("Hello"). This really confuses me as there seems to be no documentation on it.

Dose anyone have a good explanation for this some()?


System info:

  • swift -version: 4.1.2

  • Xcode: 9.4.1

wzso
  • 3,482
  • 5
  • 27
  • 48

1 Answers1

2

This appears to be a quirk in print for this compiler, purely conjecture it may be an artefact of work on changing the semantics of implicitly unwrapped optionals, see Abolish ImplicitlyUnwrappedOptional type.

The type Optional is, stripping to the basics, defined as:

enum Optional<Wrapped>
{
   case none
   case some(Wrapped)
}

Normally if you print() an enum you do get the literals, here none and some(), however print() normally prints optionals as nil and Optional().

It seems in Xcode 9.4.1 (at least) implicitly unwrapped optionals are being printed as optionals but without the special casing, whereas Xcode 9.2 (at least) prints the unwrapped value as would be expected (because it is implicitly unwrapped).

Maybe there is other interesting behaviour for implicitly unwrapped optionals in 9.4.1. You should test in Xcode 10 Beta and/or report a bug (bugreport.apple.com) in 9.4.1 and see what Apple say.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • you are right. I tried this on Xcode 10 beta 2. The printing is `Optional("Hello")`. – wzso Jun 29 '18 at 09:48
  • @0xa6a - probably not worth filing a bug against 9.4.1 then. Of course with Swift being a moving target I'm not sure it ever has bugs, just behavior at a point in time ;-) – CRD Jun 29 '18 at 22:21