What constraint for algorithms generic over real and complex numbers?

I’ve been playing around with the Numerics library, and it’s pretty great.

Sometimes I want to write a function that works for both real and complex values though, and I’m wondering what the recommended way to do so is.

Should I create a new protocol like this, and manually conform Float, Double, and Complex?

protocol MyNumberProtocol : SignedNumeric, ElementaryFunctions {
  static func / (lhs: Self, rhs: Self) -> Self
  static func /= (lhs: inout Self, rhs: Self)
}
1 Like

Yup, it's a missing feature. I'd like to get both things to be a bit more mature before trying to define it, because it'll be constantly churning if we try to pin it down too early.

I would recommend:

  • opening an issue to track it
  • making a sketch in your own project to experiment (partially for use, partially to feedback into the issue).

The protocol you've sketched is roughly [CharacteristicZero]Field; I expect that naming will require a little bit of bikeshedding at some point, too =)

4 Likes

Thanks @Nevin! Protocol for algorithms on both real and complex numbers · Issue #79 · apple/swift-numerics · GitHub