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

Brent Royal-Gordon brent at architechies.com
Sun Jan 29 22:42:04 CST 2017


> On Jan 29, 2017, at 7:02 PM, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
> 
>>>  var popcount: Int { get }
>> 
>> I'm not super-fond of this name; I assume it's a term of art, but it's
>> a pretty obscure one. Maybe `numberOfOnes`? `onesWithin`?
> 
> Yes, the rationale is that it's a term of art.  I think if you were
> going to call it something else, you'd need to mention "one _bits_" in
> the name.  If we can come up with a name that's obviously better, that's
> great, but lacking a clear winner we should go with the already-accepted
> term.

Okay, but the same could be said for `trailingZeros`. Does that also need to be something like `trailingZerosInBinaryRepresentation` or what-have-you?

>> It might make a great deal of sense to support bitwise operations on
>> this type, 
> 
> I think that's a model of SetAlgebra, then, isn't it?

Hmm, arguably. It's a shame that we won't be able to use it with things like `OptionSet`, though.

> Personally I don't think the strict separation of SetAlgebra and things
> that do bitwise operations makes sense, but I know some people feel
> strongly that it would be confusing for users to expose set operations
> with bitwise operator names.  IMO using | for union and & for
> intersection would be beautiful.  But that's a story for another day...


Honestly, if I were designing Swift from scratch, I might consider having a `BitView` type and `bits` member which acts as a collection of `Bool`s (or maybe a set of offsets of `1` bits?) and conforms to `SetAlgebra`; you could then perform bitwise operations through the `bits`, but not directly on the value itself. But that ship has clearly sailed.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list