# 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:** 12\
**Page:** 1

<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 19, 2019, 10:18pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/1 "2019-11-19T22:18:48Z")

</div>

For example:

```swift
struct Foo {
    typealias WritableKeyPath<Value> = Swift.WritableKeyPath<Foo, Value>
    var optional: Int?
    var nonOptional: Int = 0

    mutating func set<Value>(keyPath: WritableKeyPath<Value>, value: Value) {
        self[keyPath: keyPath] = value
    }
    mutating func set<Value>(keyPath: WritableKeyPath<Value?>, value: Value?, onNilOnly: Bool) {
        if self[keyPath: keyPath] == nil || onNilOnly == false {
            self[keyPath: keyPath] = value
        }
    }
}

var foo = Foo()
foo.set(keyPath: \.nonOptional, value: 1) /// foo.nonOptional: 0 -> 1
foo.set(keyPath: \.optional, value: 10, onNilOnly: true) /// foo.optional: nil -> Optional(10)
foo.set(keyPath: \.optional, value: 100, onNilOnly: true) /// foo.optional: Optional(10) <- Does not change
foo.set(keyPath: \.optional, value: 100, onNilOnly: false) /// foo.optional: Optional(10) -> Optional(100)

/// I don't want follwing code to be compiled:
foo.set(keyPath: \.optional, value: 1000) /// foo.optional: Optional(100) -> Optional(1000)

```

What I want to achieve is, when setting an optional key path, the parameter `onNilOnly` must be passed in explicitly.

So apart from changing the function name for the `Optional` one, is there a way to require the first function's generic type to be **Non-Optional**? Something like:

```swift
mutating func set<Value>(keyPath: WritableKeyPath<Value>, value: Value) where Value != Optional { ... }

```

or

```swift
mutating func set<Value?>(keyPath: WritableKeyPath<Value>, value: Value)

```

or

```swift
mutating func set<Optional<Value>>(keyPath: WritableKeyPath<Value>, value: Value)

```

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 19, 2019, 10:29pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/2 "2019-11-19T22:29:45Z")

</div>

Swift type system doesn’t have negation logic. Perhaps that’s for the best.

The best I could think of (but haven’t tested), is to provide 2 overloads, optional one, and non-optional one, and have the optional version emit some error, like deprecation.

---

<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 20, 2019, 10:55pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/3 "2019-11-20T22:55:25Z")

</div>

Yes, I thought of that too with 2 overloads. But that leads to another inconsistent behavior between non-generic function and generic function. Let me explain:

Say we have 2 strings, one optional, one non-optional:

```swift
let string: String = "Non-Optional String"
let optionalString: String? = "Optional String"

```

And a normal function `func foo1`:

```swift
func foo1(string: String) {
    print("Non-Optional String: \(string)")
}

```

Naturally, you can only pass `string` to `foo1`, and an error will prompt if you pass `optionalString` to it:

```swift
foo1(string: string) 
// prints the expected value

foo1(string: optionalString) 
// error: Value of optional type 'String?' must be unwrapped to a value of type 'String'

```

However, we can easily have another function `foo2` to accept an `Optional<String>`:

```swift
func foo2(string: String?) {
    print("Optional String: \(string as Any)")
}

```

Apparently we can pass both to this function:

```swift
foo2(string: string) 
// prints the expected value

foo2(string: optionalString) 
// prints the unwrapped value or nil

```

So we can come up with a conclusion for a normal function that takes an `Optional` parameter: unlike other non-generic functions only accept a parameter of the type it requires, it accept 2 types, the `Optional` type and the type it wraps.

However for generic functions, this logic does not exist:

```swift
func bar1<Value>(value: Value) {
    print("value: \(value)")
}

func bar2<Value>(value: Value?) {
    print("value: \(value as Any)")
}

bar1(value: string) // correct
bar2(value: string) // correct, same as previous

bar1(value: optionalString) // correct
bar2(value: optionalString) // correct, same as previous

```

In the examples above, function `bar1` and `bar2` are the same thing with a different (or is it?) implementation.

So it is clear for us to see, when it comes to generic type, `Value` and `Value?` represent the same thing, which is any type including `Optional`. And I can understand that, the `Optional` is just an another type after all, it only makes sense to make it a generic target type. But humbly I have to ask, is it correct?

First, it brings redundancy and ambiguity as the example given above shows where function `bar1` and `bar2` are the same expression.

Second, it breaks the consistency that we have learnt and got used to from the normal function, which is, when we see a parameter has a type without a "?" suffix, we know the parameter has to be unwrapped before being passed to the function; on the other hand, when we do see the "?" suffix, we know it accept 2 types, the `Optional` type and the type it wraps.

Finally, it omits the possibility to perform the delicate control over the generic type requirement. For a normal function, we can easily require the parameter to be non-optional, but we can not do such thing for generic function, because a generic type almost always include `Optional`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 20, 2019, 11:45pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/4 "2019-11-20T23:45:30Z")

</div>

> [@ylorn](#):
>
> So we can come up with a conclusion for a normal function that takes an `Optional` parameter: unlike other non-generic functions only accept a parameter of the type it requires, it accept 2 types, the `Optional` type and the type it wraps.

It is not quite so. `foo2` still take `String?` in both scenarios. What happened is that `string` is implicitly casted to `String?`, then passed as an argument. The distinction is subtle, but become more important in the next part.

> [@ylorn](#):
>
> So it is clear for us to see, when it comes to generic type, `Value` and `Value?` represent the same thing, which is any type including `Optional` . And I can understand that, the `Optional` is just an another type after all, it only makes sense to make it a generic target type. But humbly I have to ask, is it correct?

You're right that `Optional` is just another type, and so can be `Value`.  
The thing is `Value` and `Value?` doesn't represent the same thing.  
If you try to print out the `Value.self` you'll see.

```swift
let string = "Non-Optional"
let optionalString: String? = "Optional"

func bar1<Value>(_ value: Value) {
    print("\(Value.self) with value \(value)")
}

func bar2<Value>(_ value: Value?) {
    print("\(Value.self) with value \(value as Any)")
}

bar1(string) // String
bar1(optionalString) // Optional<String>

bar2(string) // String
bar2(optionalString) // String

```

So `bar1` takes the respective type, as expected.  
What's interesting is `bar2` that in both cases `Value` is `String`.  
So the specialized signature of `bar`, in both cases, is `bar2(_ value: String?)`, and again, `string` is casted to `String?`.

That is why, with some knowledge of type checking, we could use it to our advantage.  
In the simplest form, the type checker minimise the implicit casting (non-optional -\> optional among others).  
Now if you have

```swift
func bar<Value>(_ value: Value) {
  print("Non-optional")
}

@available(swift, deprecated: 0.0, message: "Use non-optional version instead")
func bar<Value>(_ value: Value?) {
  print("Optional")
}

bar(string)
bar(optionalString)

```

The `optionalString` case is simple, it can't use the top one.  
The `string` case is more interesting.  
If the compiler decide to use bottom `bar`, it'll have to do 1 implicit conversion (`string` to `String?`).  
If the compiler decide to use the top one, there will be no conversion.  
So the compiler will always choose the top `bar` for non-optional variable.

It's probably not ideal for some use case. Though I don't think negation constraint will come any time soon. Negation in type system is a rather complex problem.

---

<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 21, 2019, 12:18am UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/5 "2019-11-21T00:18:07Z")

</div>

> [@Lantua](#):
>
> What's interesting is `bar2` that in both cases `Value` is `String` .

Wow, I did not see this.

> [@Lantua](#):
>
> I don't think negation constraint will come any time soon. Negation in type system is a rather complex problem.

I agree what you said in the previous thread that is for the best. And what I am feeling now is not the need for a negation constraint (maybe I did when I posted the topic...), but a sense of loss of the absence of a level of strictness. Taking file access permission as an example, you can either have **read** permission or **read and write** permission. The lacking of the **read-only** is what now I feel wired about.

Thanks a lot for the detailed walkthrough and explanation!

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 21, 2019, 12:25am UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/6 "2019-11-21T00:25:01Z")

</div>

Another design angle is to have

- `setIfNil(optional:)` vs `set(nonOptional:)` instead of
- `set(optional:ifNil:)` vs `set(nonOptional:)`.

---

<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, 3:58pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/7 "2019-11-22T15:58:42Z")

</div>

I have updated my API as per @Lantua 's suggestion, and it works as intended now.

But I believe there is still a bug in Swift:

```swift
func bar<T>(_ v: T) {
    if v == nil {
        print("\(v) is nil.")
    } else {
        print("\(v) is NOT nil.")
    }
}

let nilInt: Int? = nil
bar(nilInt)

```

I think the `if` block got optimized away and `else` block got executed directly, because it prints:

> nil is NOT nil.

## Update:

A version **without** the warning of:

> Comparing non-optional value of type 'T' to 'nil' always returns false

```swift
func bar<T>(_ v: T) {
    if (v as Any?) == nil {
        print("\(v) is nil.")
    } else {
        print("\(v) is NOT nil.")
    }
}

let nilInt: Int? = nil
bar(nilInt)

```

Still prints:

> nil is NOT nil.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [November 22, 2019, 4:23pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/8 "2019-11-22T16:23:46Z")

</div>

> [@ylorn](#):
>
> But I believe there is still a bug in Swift:

Your example works correctly and as intended.

The static type of `v` is `T`, which has no constraints. In particular, `T` is not `Optional`, `T` is not `ExpressibleByNilLiteral`, and `T` is not `Equatable`.

The only way for the compiler to make sense of `v == nil` is to promote `v` from `T` to `T?`. It then uses the special overload of `==` that compares an optional of non-`Equatable` type to the `nil` literal (specifically, the right-hand side is `_OptionalNilComparisonType`).

However, since `v` was promoted from `T` to `T?`, it follows that the resulting `T?` is not `nil`—it cannot be `nil`, because it is exactly `T?.some(v)`.

When you call your function with `nil as Int?` (aka `Int?.none`), the compiler understands that `T` is `Int?`. That means `T?` is `Int??`, and so the `==` operator ends up comparing `Int??.some(v)` with `nil as _OptionalNilComparisonType`.

And that comparison always returns `false`, because, again, `Optional(v)` is definitely not `nil`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 22, 2019, 4:25pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/9 "2019-11-22T16:25:57Z")

</div>

It looks like this:

```swift
let v: Int? = nil
let promoted: Int?? = .some(v)
print(promoted == nil) // false

```

Implicit conversion can be surprising if you're not careful. In this case, you'll see a warning

```swift
Comparing non-optional value of type 'T' to 'nil' always returns false

```

Which is as described by @Nevin.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 22, 2019, 4:33pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/10 "2019-11-22T16:33:42Z")

</div>

Also, if you instead do `T?`, you'll get what you expected

```swift
func bar<T>(_ v: T?) {
    if v == nil {
        print("\(v) is nil.")
    } else {
        print("\(v) is NOT nil.")
    }
}

let nilInt: Int? = nil
bar(nilInt) // nil is nil

```

This is because you pushed the promotion to the `bar` call site. So if `v` is already optional, there will be no promotion.

---

<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.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [November 23, 2019, 5:40pm UTC](https://forums.swift.org/t/generic-function-that-requires-the-generic-type-to-be-non-optional/30936/12 "2019-11-23T17:40:31Z")

</div>

I will note that there is _something_ funky going on, because replacing `Optional<Any>.none` with `Any?.none` in your code gives an error.

Thus:

```swift
func foo(_ x: Any) {
  switch x {
  case Optional<Any>.none : print("Optional<Any>.none")
  case .none as Any? /* 1 */ : print(".none as Any?")
  case nil as Any? /* 2 */ : print("nil as Any?")
  case Any?.none /* 3 */ : print("Any?.none")
  default : print("default")
  }
}

// 1 & 2 both give warning: Case is already handled by previous patterns; consider removing it
// 3 gives error: Expression pattern of type 'Any?' cannot match values of type 'Any'

```
