Numeric.zero trouble

Hello. I have trouble with Numeric.zero property.
I try to check distance between a center minus radius of a body and the beginning of the outer box i.e zero point of coordinates. And I have the error. Can you please explain, what exactly does the static zero property in the Numeric protocol?

protocol Position {
    associatedtype Measure: Numeric
    var x: Measure { get set }
    var y: Measure { get set }
    var radius: Measure { get }
}

protocol Boxing {
    associatedtype Measure: Numeric
    var width: Measure { get }
    var height: Measure { get }
}

struct Forum {

    func checkDistance<Item: Position, Box: Boxing>(item: Item, box: Box)
        where Item.Measure == Box.Measure
    {
        let edge = item.x - item.radius
        if edge < Box.Measure.zero { /* collision */ }
    
    /// Cannot convert value of type 'Item.Measure' to expected argument type 'Unicode.CanonicalCombiningClass'
}

}

OH I am sorry.
associatedtype Measure: Numeric, Comparable, fix the problem. I did not expect that numeric is not comparable.

That is still a spectacularly poor diagnostic, though. Worth filing a bug report about IMO.

We could detect when you try and use very common operators (like for Equatable/Comparable) on values of an associated type which aren't constrained to require those protocols.

3 Likes

Now it is there. This note helped me.

struct Forum {
    func checkDistance<Item: Position, Box: Boxing>(item: Item, box: Box)
        where Item.Measure == Box.Measure
    {
        let edge = item.x - item.radius
        if edge > box.width {}
       // Binary operator '>' cannot be applied to two 'Item.Measure' operands
    }
}

A problem is only with zero property.

1 Like
Terms of Service

Privacy Policy

Cookie Policy