Open swift-ci opened 7 years ago
The first inference seems reasonable, but the others are not. Uploading your pages as standalone Swift files.
Ah, it looks like the second page is now also accepted, which seems reasonable as well. So it's just about the default case.
protocol Foo {
associatedtype Bar = Int
func bar() -> Bar
}
extension Foo where Bar == String {
func bar() -> String {
return "hello"
}
}
extension Foo where Bar == Int {
func bar() -> Bar {
return 108
}
}
struct Piyo: Foo {
// Bar is String...
}
print(Piyo().bar())
@swift-ci create
Interestingly, if I change the second extension to return Int
instead of Bar
then it becomes ambiguous.
Comment by Nobuo Saito (JIRA)
@belkadan
Thank you for checking,
In my opinion, this inference has two problems.
1. Undecidable inference,
In first case, the following extension works the default Bar,
extension Foo where Bar == String {
func bar() -> String {
return "hello"
}
}
but in my opinion it should not affect default `Bar`.
`Bar` is determined to be a `String` by function implement, but It should work only `Bar` is `String` case.
There aren't any other information, it seems don't affect `Bar` type is reasonable, maybe.
2. Different behavior by type declaration.
If I write function implement using `Bar` instead of `String` like,
extension Foo where Bar == String {
func bar() -> Bar {
return "hello"
}
}
then, in first case it's require that define `Bar` type. It seems this behavior is better than 1. case.
Attachment: Download
Environment
Xcode Version 8.3.2 (8E2002) Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42), Target: x86_64-apple-macosx10.9Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 31385fa142ec2bae214f5e8af12d7e56Issue Description:
If we extend a protocol restrict some case, and write default implementation of function that referencing associated type, we can find some of strange type inferences.
Minimum code is here
And I find some of other type inferences also, I attach MyPlayground.zip.