Renaming bitPattern and truncatingBitPattern

I propose upgrading bit-casting labels to match the new API design
guidelines. Here's what we have today:

UInt8(bitPattern: someInt8)
Uint8(truncatingBitPattern: someBiggerInt)

"Bit pattern" has the needless word "pattern" while not reading well due to
the lack of a verb or preposition. Simply "bits" will not be confused with
something else in this context. "Truncating" is the present participle
which is confusing.

I like:
UInt8(fromBits: someInt8)
Uint8(truncateBits: someBiggerInt)

-david

···

On Tue, Feb 16, 2016 at 11:29 AM, Dave Abrahams via swift-evolution < swift-evolution@swift.org> wrote:

on Tue Feb 16 2016, David Turnbull <swift-evolution@swift.org> wrote:
>
> My last week was spent reading files with huffman coding. So I had no
> choice but to use bitwise operations. My experience is that Swift got
this
> right (except for "truncatingBitPattern" taking up 25% of an 80 column
> line).

Suggestions for improved names welcomed.

More brainstorming:

UInt8(bitsOf: someInt8)
UInt8(byTruncating: someBiggerInt)

···

On Tue, Feb 16, 2016 at 1:37 PM, David Turnbull via swift-evolution < swift-evolution@swift.org> wrote:

I propose upgrading bit-casting labels to match the new API design
guidelines. Here's what we have today:

UInt8(bitPattern: someInt8)
Uint8(truncatingBitPattern: someBiggerInt)

"Bit pattern" has the needless word "pattern" while not reading well due
to the lack of a verb or preposition. Simply "bits" will not be confused
with something else in this context. "Truncating" is the present participle
which is confusing.

I like:
UInt8(fromBits: someInt8)
Uint8(truncateBits: someBiggerInt)

-david

On Tue, Feb 16, 2016 at 11:29 AM, Dave Abrahams via swift-evolution < > swift-evolution@swift.org> wrote:

on Tue Feb 16 2016, David Turnbull <swift-evolution@swift.org> wrote:
>
> My last week was spent reading files with huffman coding. So I had no
> choice but to use bitwise operations. My experience is that Swift got
this
> right (except for "truncatingBitPattern" taking up 25% of an 80 column
> line).

Suggestions for improved names welcomed.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don't think any of these are as obvious as the current names. “bitPattern”
says, “give the result this bit pattern,” rather than simply “use these
bits (somehow).” Likewise you could ask, “in what way are these bits
being truncated?”

These names are long, but IMO the clarity is worth it considering that
they're comparitively-rarely used.

···

on Tue Feb 16 2016, David Turnbull <dturnbull-AT-gmail.com> wrote:

I propose upgrading bit-casting labels to match the new API design
guidelines. Here's what we have today:

UInt8(bitPattern: someInt8)
Uint8(truncatingBitPattern: someBiggerInt)

"Bit pattern" has the needless word "pattern" while not reading well due to
the lack of a verb or preposition. Simply "bits" will not be confused with
something else in this context. "Truncating" is the present participle
which is confusing.

I like:
UInt8(fromBits: someInt8)
Uint8(truncateBits: someBiggerInt)

--
-Dave

1 Like

The existing names seems as bad as the alternatives proposed. I don't understand what do they do even after reading documentation :-[
Do they work is the parameter is in the same endianness as the result or what?
What happens when you truncate a 64 int that does not fit into 32 bit?
Do you get a runtime exception or what?
Maybe I'm bad at reading the documentation. Diagonally.

1 Like

I'm only able to find the truncatingBitPattern: initializer in the docs for really old Swift versions, <4.0. The modern equivalent is init(truncatingIfNeeded:). In each case, the docs are clear as to what they do; in the case that the original value is too large to represent, it takes only the least significant bits. There are some pretty good examples here.

If people want to start a new discussion about this, please start a new thread.