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

Dave Abrahams dabrahams at apple.com
Mon Jan 30 00:07:21 CST 2017


on Sun Jan 29 2017, Brent Royal-Gordon <brent-AT-architechies.com> wrote:

>> 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?

Fair enough.  trailingZeroBits would be fine.  “InBinaryRepresentation”
isn't accurate enough; it would have to be “InTwosComplement,” but I'd
rather leave that out of the name and let the documentation clarify that
it's all 2's complement, all the way down.

>>> 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.

Why not?  That conforms to SetAlgebra.


>> 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.

-- 
-Dave


More information about the swift-evolution mailing list