Skip to content

[SR-14478] Optional<T>.none is accepted, but nil is not #56834

Open
@swift-ci

Description

@swift-ci
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationimprovementliteralsFeature → expressions: Literals such as an integer or string literalnilFeature → expressions → literals: The `nil` literalswift 5.9type checkerArea → compiler: Semantic analysistype inferenceFeature: type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions