Is there some Number protocol which covers Int, Float, Double, etc.?

I often write wee convenience functions to do a simple bit of math or comparison between two numeric values. For instance, given a value and a pair of thresholds, it might return an enum saying whether it's under, between, or over the thresholds.

Is there a simple way to declare a function like

func inThreshold<T: NumberThing>(value: T, lower: T, upper: T) -> ThresholdEnum...

where NumberThing is essentially Numeric + Comparable?

Is this not good enough?

func inThreshold<T: Numeric & Comparable>(value: T, lower: T, upper: T) -> ThresholdEnum...
1 Like

Oooooh, I'm not sure I realized that was a possibility.

(Edited to add an intended negation operator)

1 Like

I recommend taking a closer look at Numeric and figuring out whether you actually need it. The number protocols aren’t exactly flawless, but they do break out enough functionality that you can be slightly more specific.

Terms of Service

Privacy Policy

Cookie Policy