Skip to content

[SR-7524] Non-accurate ambiguity error #50066

@swift-ci

Description

@swift-ci
Previous ID SR-7524
Radar None
Original Reporter gmosx (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @xedin
Priority Medium

md5: 1ff3c4a77acedeb5a72b9815c51ccc1d

Issue Description:

Reproduction scenario:

extension Sequence where Element: Numeric {
    public func sum() -> Element {
        return reduce(0, +)
    }
}

extension Sequence {
    public func sum<T>() -> T where T: Numeric, Element == T? {
        var sum: T = 0

        for value in self {
            if let value = value {
                sum += value
            }
        }

        return sum
    }
}

extension DataSeries: BidirectionalCollection {
    public typealias Value = Any?
    public typealias Element = Value
    ...
}

let series: DataSeries

let s = series.sum()

yields the following not very helpful error:

*****:70:24: error: ambiguous reference to member 'sum()'
        XCTAssertEqual(series.sum(), 948.872)
                       ^~~~~~
Statistics.Sequence:2:17: note: found this candidate
    public func sum() -> Self.Element
                ^
Statistics.Sequence:2:17: note: found this candidate
    public func sum<T>() -> T where T : Numeric, Self.Element == T?

I suspect the real error is that series is a Sequence of Any? instead of Numeric?

For more context, please see this thread on forums.swift.org:

https://forums.swift.org/t/generic-method-over-a-sequence-of-optionals/12106

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions