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:
This is mentioned in the Generics Manifesto as "Conditional Conformances
via Protocol Extensions": <
While it would be a very powerful and useful feature, it's apparently also
very difficult unfortunately :( One of the biggest issues with
implementing it would be the effects that is has on dynamic type checking
for protocols, and there's a good discussion about it on the list that's
better than anything I could write: < https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160905/027032.html
···
On Fri, Sep 22, 2017 at 1:53 PM Nevin Brackett-Rozinsky via swift-evolution <swift-evolution@swift.org> wrote:
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: