Unsigned byte sidestep - introduction
Bruce Chapman
brucechapman at paradise.net.nz
Wed Mar 25 01:43:47 PDT 2009
Hello all,
I am about to submit 3 proposals which are related.
This email serves to background the 3 proposals
The proposals are my best attempt at solving the no unsigned byte
dilemma in a way that fits the existing language.
After much thought I have identified two root issues which cause me to
want unsigned byte.
The proposals address these issues without actually add unsigned bytes.
The first root issue is the lack of byte size literals, so byte literals
must be formed by a narrowing conversion (cast) from an int literal. The
cast in this case can increase the code by over 100% and is particularly
onerous when populating byte arrays with fixed values, often during
testing but in other cases as well.
I have two competing proposals for byte (and short) integer literals
with different advantages and disadvantages. These are mutually
exclusive proposals (but under the coin rules they can't really refer to
each other - hence this email). One of these has a nicer syntax at the
expense of a more complex specification and narrower focus (it doesn't
support decimal byte literals).
The second root issue is the automatic widening conversion to int
whenever a byte field or variable is used in an expression. It is not so
much the widening, but the implicit sign extend which causes problems
which bite when least expected. The third proposal adds an operator
(cast like operator) to convert byte (and short) to int with zero extend
to reduce the pain of the second issue.
Here is an example showing some of the present traps.
byte b = (byte)0x80; // cast needed because 0x80 is int and wont fit
// in signed bit
assert b == 0x80; // throw AssertionError because == causes the value
// of b to be converted to int value 0xFFFFFF80
Here are the google doc references, I'll post content soon.
Auto Typing Hexadecimal and Binary Integer Literals : 0hFF
http://docs.google.com/Doc?id=dcvp3mkv_125ww5tct
Byte and Short Integer Literals (new type suffixes) : 0xFFy
http://docs.google.com/Doc?id=dcvp3mkv_0fvz5gx7b
Unsigned Integer Widening Operator : (+)
http://docs.google.com/Doc?id=dcvp3mkv_2k39wt5gf
Bruce
More information about the coin-dev
mailing list