Home > Blockchain >  false as Bool return nil instead of Optional(false)
false as Bool return nil instead of Optional(false)

Time:12-22

Recently joined a new project and noticed, in project expression false as Bool? returns nil instead of Optional(false).

XCode 14.1, 14.2. Same expression in playground returns Optional(false). All swift compiler optimizations are disabled. What could case this behavior?

enter image description here

CodePudding user response:

Someone redefined init(booleanLiteral:) in the project.

extension Bool?: ExpressibleByBooleanLiteral {
  public init(booleanLiteral bool: Wrapped) {
    self = bool ? bool : nil
  }
}
  • Related