swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-50] Use 'try' statement to guide type inference

Open akyrtzi opened this issue 11 years ago • 1 comments

Previous ID SR-50
Radar None
Original Reporter @akyrtzi
Type Improvement
Status Reopened
Resolution

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, TypeChecker
Assignee None
Priority Medium

md5: 9004a0727c0e0a404b014393e17c9bce

Issue Description:

With this code:

enum SomeError : ErrorType {
  case Error
}

enum SomethingOrError<T> {
  case err(SomeError)
  case some(T)
}

func doit() -> SomethingOrError<Int> {
  return SomethingOrError.some(0)
}

func doit() throws -> Int {
  return 0
}

let something = try! doit()

You get:

t.swift:18:22: error: ambiguous use of 'doit()'
let something = try! doit()
                     ^
t.swift:10:6: note: found this candidate
func doit() -> SomethingOrError<Int> {
     ^
t.swift:14:6: note: found this candidate
func doit() throws -> Int {
     ^

Consider guiding type inference to resolve the call to the throwing function since the call uses 'try'.

akyrtzi avatar Dec 04 '15 17:12 akyrtzi

Since you're allowed to put try around a whole expression, I'm not convinced this is (1) implementable, or (2) a good idea. I don't think we can reasonably count try as a "contextual type" that implies something throws.

belkadan avatar Dec 04 '15 17:12 belkadan