RFR: 8297539: Consolidate the uses of the int<->float union conversion trick
Justin King
jcking at openjdk.org
Thu Feb 2 22:25:35 UTC 2023
On Thu, 2 Feb 2023 21:59:17 GMT, John R Rose <jrose at openjdk.org> wrote:
> I think the term "cast" in this API means too many different things, including things that a C programmer would not think of as simple casting. To be clear, this is a problem I have with the existing API, not just this PR.
>
> I request we start to make a clearer distinction between conversions which preserve values (perhaps with truncation or loss of precision) and reinterpretations which discard values but keep the underlying bits. Both may be called casts, but it is asking for bugs to use the name for both kinds of requests.
>
> (Using the word cast for something that preserves at least some values is how the JLS handles conversion terminology, and the JLS makes reinterpret casts available only via special methods. I know C++ has its own lexicon of terms, but I claim some privilege on this project for Java terms as well.)
>
> I know this work here is changing from names like "jint_cast" which perform reinterpretations, but I always viewed those names as suspicious, and had to learn by rote that they refer to reinterpretations and not true C (or Java) casts. Naming it just "cast" with a tricky type parameter is a move even farther in the wrong direction.
>
> Having the function named "cast" in a class named PrimitiveConversions (not PrimitiveReinterpretations) makes it dangerously easy to miss that the functions in that API are (apparently?) all reinterpretations, and none are what a Java programmer would call a cast. I suggest either renaming of the class (PrimitiveReinterpretations) or better changing the name from "cast" to something like "reinterpret". Then casual readers of code will be less likely to make errors of… reinterpretation.
It should be noted that bit_cast is the chosen term for C++ starting in C++20, which reinterprets. Whether that is clear or not is a separate issue, but using `cast` is consistent at least even if odd. Maybe we just change it to `Bits::cast`?
-------------
PR: https://git.openjdk.org/jdk/pull/12384
More information about the hotspot-dev
mailing list