swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.62k stars 10.37k forks source link

[SR-1302] Type inference from Void return type. #43910

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-1302
Radar None
Original Reporter bnut (JIRA User)
Type Improvement
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Improvement, LanguageFeatureRequest, TypeChecker | |Assignee | None | |Priority | Medium | md5: 7b1d93393e0ef2686685b69a17a3bfec

Issue Description:

Overview

The return type is used to disambiguate function resolution. Whether a functions result is used should be used by the type-checker to disambiguate between Void and non-Void functions.

Consistency

Examples

The following examples use this code:

func test(x: Double) {
    print("no return")
}
func test(x: Double) -> Int {
    print("with return")
    return Int(x)
}
func noproblem(x: Double) -> Double {
    print("no problem")
    return x
}

Currently this compiles, it uses the "no return" version:

[1.0, 2.0, 3.0].forEach { x in
    test(x)
}

This fails to compile, error: ambiguous use of 'test', I expect it to use the "with return" version:

let values: [Double] = [1.0, 2.0, 3.0]
let x = values.map { x in
    test(x)
}

This fails to compile, error: ambiguous use of 'test', I expect it to use the "no return" version:

test(123.0)

Currently this has no error, that should still be the case, if it's unambiguous:

noproblem(123.0)

Thanks!

belkadan commented 8 years ago

As a language change, this would need to go through the Swift Evolution Process.

swift-ci commented 8 years ago

Comment by Andrew Bennett (JIRA)

Thanks Jordan, I started a thread a while ago, but it didn't get any responses. I guess it got lost in the storm, or I submitted it at the wrong time (I'm in Australia).

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160328/013559.html

I wasn't sure if this was a new feature, or a fix on an existing one, so I thought I'd try here.