Why `Double` can pass to `CGFloat?`, but `Double?` cannot?

We did it only for value -> optional because optional <-> optional conversions are hard to represent via the mechanism we are using to implement this (calling Double.init or CGFloat.init implicitly) because there could be multiple levels of optionality that have to be matched. I guess one way to implement that might be to use linked flatMap implicitly e.g.

import Foundation

func test(v: Double??) -> CGFloat?? {
  v.flatMap { $0.flatMap { CGFloat($0) } }
}

Might be worth opening a bug on bugs.swift.org as a refinement on the current implementation.

7 Likes