Open
Description
Previous ID | SR-14478 |
Radar | rdar://problem/76598223 |
Original Reporter | ken (JIRA User) |
Type | Improvement |
Additional Detail from JIRA
Votes | 0 |
Component/s | |
Labels | Improvement |
Assignee | None |
Priority | Medium |
md5: 559996b58a5c9717778c0de4a68d738f
Issue Description:
There are some situations I've found where nil
doesn't seem to be considered a valid Optional
, but spelling out Optional<T>.none
works fine. Extending Optional
seems to confuse the compiler. For example:
$ xcrun swift
Welcome to Apple Swift version 5.3 (swiftlang-1200.0.29.2 clang-1200.0.30.1).
Type :help for assistance.
1> protocol HotDogOrNot { }
2> protocol HotDog: HotDogOrNot { }
3> extension Optional: HotDogOrNot where Wrapped == HotDog { }
4> func f(_ x: HotDogOrNot) { }
5> f(nil)
error: repl.swift:5:3: error: 'nil' is not compatible with expected argument type 'HotDogOrNot'
f(nil)
^
5> f(Optional<HotDog>.none)
6> nil is HotDogOrNot
error: repl.swift:6:5: error: type of expression is ambiguous without more context
nil is HotDogOrNot
~~~~^~~~~~~~~~~~~~
6> Optional<HotDog>.none is HotDogOrNot
$R0: Bool = true
7> Optional<HotDog>.none == nil
$R1: Bool = true
I really have no intuition for whether this would be easy or hard to implement. From the user's point of view, it would be nice if Optional<T>.none
were always able to be replaced by nil
in source code.
Maybe part of #56764.