[swift-evolution] [Re-Review] SE-0104: Protocol-oriented integers

David Sweeris davesweeris at mac.com
Wed Feb 22 10:47:02 CST 2017


> On Feb 22, 2017, at 8:01 AM, Ben Cohen via swift-evolution <swift-evolution at swift.org> wrote:
> 
> There is another option to avoid the extra types, which is to stop trying to force the disambiguation through argument labels, accept an ambiguous call and have the context disambiguate:
> 
> // compilation error: ambiguous
> let x = i.multiplied(by: j)
> // unambiguously overflow-checked
> let (y,overflow) = i.multiplied(by: j)
> // unambiguously full-width
> let z: DoubleWidth = i.multiplied(by: j)
> 
> Ambiguity is bad when you want to distinguish between the “usual one” versus other more specialized versions. So if you really had a regular trapping `adding`, but then also wanted to accommodate the overflow-reporting version when a user explicitly requests it, then the argument label is a clear win. This is a slightly bogus example though, because we explicitly don’t have things like `adding`, we have a `static +` instead. Where the disambiguation is needed is instead between two less common variants as described above.

It’d need a new language feature to support it, but what having “default” resolutions for overloaded functions?
    default func multiplied(by other: Self) -> Self // `default` means try resolving ambiguities with this version first. The overloaded versions are only considered if the type-checker can’t make this version work.
    func multiplied(by other: Self) -> (partialValue: Self, overflow: ArithmeticOverflow)
    func multiplied(by other: Self) -> DoubleWidth<Self>

// signature matches default implementation, use that 
let x = i.multiplied(by: j)
// default version doesn’t return a tuple, so try the overloads… matches the overflow-checked function
let (y,overflow) = i.multiplied(by: j)
// default version doesn’t return a DoubleWidth, so try the overloads… matches the double-width function
let z: DoubleWidth = i.multiplied(by: j)

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170222/99b02110/attachment.html>


More information about the swift-evolution mailing list