Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8318] Optional generic return type not treated as specialised #50846

Open
swift-ci opened this issue Jul 19, 2018 · 1 comment
Open

[SR-8318] Optional generic return type not treated as specialised #50846

swift-ci opened this issue Jul 19, 2018 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8318
Radar None
Original Reporter Xander (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: d13cadc615b655a51990e1aa197ad547

Issue Description:

Two generic functions that differ in that the return values are optional vs non-optional cause an ambiguity error when the type will resolve as an optional.

Example:

import Foundation

extension Data {
    // Get the data value back as a certain type
    func toValue<T>() -> T {
        // Do something for a non-optional return
        return self.withUnsafeBytes { $0.pointee }
    }

    func toValue<T>() -> T? {
        // Do something for an optional return
        return nil
    }
}

let value: Int = Data().toValue()

let value: Int? = Data().toValue() // Ambiguous use of 'toValue()'

The optional return type should be used, just as it would be with a function parameter, as it is more specialised.

@belkadan
Copy link
Contributor

cc @rudkx

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants