# Generic function that requires the generic type to be non-optional

**URL:** <https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936>\
**Category:** Using Swift\
**Tags:** generics\
**Created:** [November 19, 2019, 10:18pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936 "2019-11-19T22:18:47Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![ylorn](https://avatars.discourse-cdn.com/v4/letter/y/13edae/32.png) [@ylorn](https://forums.swift.org/u/ylorn)\
**Post date:** [November 22, 2019, 5:27pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/11 "2019-11-22T17:27:55Z")

</div>

Reply to @Nevin and @Lantua

> [@Nevin](#):
>
> The only way for the compiler to make sense of `v == nil` is to promote `v` from `T` to `T?` .

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?`:

```swift
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](https://forums.swift.org/t/challenge-flattening-nested-optionals/24083))  
Thanks guys.

---

_[View the full topic](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936)._
