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-4347] Improve inference of optional supertypes #46926

Closed
swift-ci opened this issue Mar 24, 2017 · 1 comment
Closed

[SR-4347] Improve inference of optional supertypes #46926

swift-ci opened this issue Mar 24, 2017 · 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-4347
Radar None
Original Reporter tonisuter (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee tonisuter (JIRA)
Priority Medium

md5: 20d0e6f1bcce37e45b35dd11807d17ac

Issue Description:

If an array literal contains at least one element that is a nil literal and at least one other element that is of type T, the type checker will set the element type of the array to Optional<T>:

let arr1 = [1, nil, 3]
print(type(of: arr1))       // Array<Optional<Int>>

struct S {}
let arr2 = [S(), nil]
print(type(of: arr2))       // Array<Optional<S>>

This also works in other situations, where the type checker tries to find a common supertype of multiple expressions:

let x1 = true ? 1 : nil
print(type(of: x1))     // Optional<Int>

struct S {}
let x2 = false ? nil : S()
print(type(of: x2))     // Optional<S>

However, this currently only seems to work if T is a nominal type. If T is any other kind of type, there's a type error:

let arr = [(1, "test"), nil, (2, "test")]   // error: type of expression is ambiguous without more context

let c = { $0 * 2 }
let x = true ? c : nil          // error: result values in '? :' expression have mismatching types '(Int) -> Int' and '_'

There is no reason why this shouldn't work with other types as well.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 6, 2017

Comment by Toni Suter (JIRA)

This has been fixed in 96b01c4

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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

1 participant