Float16 v CGFloat + Mirror

Both Float16 and CGFloat are "struct".
Why does Mirror API treat them differently?

func printMirror<T>(_ v: T) {
    let mirror = Mirror(reflecting: v)
    print("\ndisplayStyle: \(String(describing: mirror.displayStyle))")
    print("subjectType: \(String(describing: mirror.subjectType))")
    print("children count: \(mirror.children.count)")
    mirror.children.forEach { child in
        print("    \(String(describing: child.label)) \(child.value)")
    }
}
printMirror(Float16(0))

displayStyle: Optional(Swift.Mirror.DisplayStyle.struct)
subjectType: Float16
children count: 1
Optional("_value") (Opaque Value)

printMirror(CGFloat(0))

displayStyle: nil
subjectType: Double
children count: 0

CGFloat is bridged to Swift as a struct, but it's not a native Swift type. CGFloat is originally defined by the Core Graphics framework as an alias for C value primitives (either float or double, depending.)

Float16 on the other hand is a native Swift type and Swift is free to define it as it sees fit, so long as it does what it's supposed to (store a 16 bit float value.) In this case that Swift has chosen to implement it (and probs other number types) as a struct with an internal _value property.

4 Likes