[swift-evolution] protocol-oriented integers (take 2)
Anton Mironov
antonvmironov at gmail.com
Wed Jan 25 13:36:52 CST 2017
I wish I knew how to express those requirements in the type system.
This is a good way to implement additiveIdentity but I assume that Arithmetic protocol implies behavior of Addable.
> On Jan 25, 2017, at 20:58, David Sweeris <davesweeris at mac.com> wrote:
>
>>
>> On Jan 25, 2017, at 07:59, Anton Mironov via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> Hi everyone,
>>
>> I want to suggest a tiny extension to an Arithmetic protocol. It would be nice to have an additive identity and a multiplicative identity constants. Basically zero and one.
>>
>> ```
>> protocol Arithmetic {
>> /* ... */
>> static var zero: Self { get } // additive identity: (value + .zero) == value
>> static var one: Self { get } // multiplicative identity: (value * .one) == value
>> }
>> ```
>>
>> These constants will ease implementation of math structures: vectors, matrices and etc.
>> I’m sorry if I’m duplicating someone’s suggestion. It is really hard to search for something in a long thread.
>
> Vectors, matrices, etc can't conform to this protocol anyway because of the * and / requirements. And while it is true that it's not uncommon to reference an all-zero matrix or vector as "0", that doesn't work for any other number... for them I think the spelling should be "additiveIdentity". It'll be easy enough to just say
> protocol Addable {
> static var additiveIdentity: Self {get}
> ...
> }
> extension Addable where Self: Arithmetic {
> static var additiveIdentity: Self {return Self.zero}
> }
>
> IMHO, anyway.
>
> - Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170125/56e2c555/attachment.html>
More information about the swift-evolution
mailing list