So the v == nil
will silently convert T
to T?
. This is easy to overlook. I guess casting T
to Any
and then perform the case checking should avoid the silent cast from T
to T?
:
func bar<T>(_ v: T) {
if case Optional<Any>.none = v as Any {
print("\(v) is nil.")
} else {
print("\(v) is NOT nil.")
}
}
let nilInt: Int? = nil
bar(nilInt)
It now prints:
nil is nil.
(or the one from this thread)
Thanks guys.