Bugs of Swift 5

Here are the story. I updated my library yesterday and found some of bugs with Swift 5.
These are the part of code that reproduce the problem.

public protocol ScalarProtocol: ScalarMultiplicative where Self == Scalar {
    
}

public protocol ScalarMultiplicative : AdditiveArithmetic {
    
    associatedtype Scalar : ScalarProtocol
    
}

public protocol MapReduceArithmetic : ScalarMultiplicative, Collection where Element : ScalarMultiplicative, Scalar == Element.Scalar {
    
    func map(_ transform: (Element) -> Element) -> Self
    
    func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result
    
    func reduce<Result>(into initialResult: Result, _ updateAccumulatingResult: (inout Result, Element) -> ()) -> Result
}

extension Collection where Self : MapReduceArithmetic {
    
    public func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result {
        return self.reduce(into: initialResult) { $0 = nextPartialResult($0, $1) }
    }
}

The protocol MapReduceArithmetic is an implementation details and provide a default operators implementation. You can found the example in here.

However, if i create a new protocol base on MapReduceArithmetic and limiting with Element == Scalar.
The compiler is going wrong.

public protocol Tensor : MapReduceArithmetic where Scalar : BinaryFloatingPoint, Element == Scalar, Index == Int {
    
    var magnitude: Scalar { get set }
}

extension Tensor {
    
    public var magnitude: Scalar {
        return self.reduce(0) { $0 + $1 * $1 }.squareRoot()
    }
}

Is that bugs of compiler? If yes, i will file this bug later.

also i reported other bugs which may related to this.

https://bugs.swift.org/browse/SR-10022
https://bugs.swift.org/browse/SR-10023

Terms of Service

Privacy Policy

Cookie Policy