[swift-evolution] Retroactive protocol inheritance

Nevin Brackett-Rozinsky nevin.brackettrozinsky at gmail.com
Fri Sep 22 15:53:32 CDT 2017


With Swift 4 out, I’ve started using the numeric protocols quite a bit, and
they are great!

One thing I find myself wishing for is a protocol that extends Numeric
while also allowing division—a Field protocol, if you will. I have
implemented several algorithms generically over FloatingPoint because they
need division, which means they aren’t available for, eg., a Rational type.

Absent a Field protocol in the standard library, I can create one of my own:

protocol Field: Numeric {
  static func / (lhs: Self, rhs: Self) -> Self
}

And I can extend Float and Double and Float80 to conform easily enough.
However, I’d really like to write,

extension FloatingPoint: Field {}

and make every type which conforms to FloatingPoint (such as a third-party
Complex type), also conform to Field.

Is this potentially feasible? Would other people find it useful?

Nevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170922/77009e7d/attachment.html>


More information about the swift-evolution mailing list