[swift-evolution] protocol-oriented integers (take 2)

Steve Canon scanon at apple.com
Wed Jan 25 12:55:35 CST 2017



Sent from my iPhone

> On Jan 25, 2017, at 1:45 PM, David Sweeris via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Jan 25, 2017, at 10:03, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Stephen Canon wrote that Arithmetic should refine ExpressibleByIntegerLiteral because of certain mathematical properties of rings. In that case, 0 and 1 would just be spelled in that way. Otherwise, +1.
>>> On Wed, Jan 25, 2017 at 11:38 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.
> 
> There is some merit in having them be declared as static properties... If it's a non-trivial process to initialize a type, it might be worth it to have a static property that either provides the storage directly or just returns a fileprivate value declared outside the type.

A type for which this was necessary can just special-case handling 0 and 1 literals.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170125/68f124ea/attachment.html>


More information about the swift-evolution mailing list