-
Notifications
You must be signed in to change notification settings - Fork 44
fix primitive constants #264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cd4fbe4
to
673db7f
Compare
TL;DR I agree with the basic idea. There are some details to work out though. Longer explanation & background: As you guessed, I left off these types initially because they are all represented as What complexity you may ask? Well... Relying on So, to do this right, we need to do a couple of things:
This way, no matter what weird way the user sends in a Byte, Short, etc., we always get the same correctly-typed constant out of it. I hope this makes sense! |
Yeah, I originally started doing something like that, but @mkouba's comment that |
673db7f
to
a8913c7
Compare
Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good other than two minor questions. Thanks!
* @param value the value to create a constant from | ||
*/ | ||
static Constant of(byte value) { | ||
return ConstantImpl.of((Byte) value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These methods could just be ConstantImpl.of(value)
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, will fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -98,6 +101,18 @@ public StringConstant stringConstant(String value) { | |||
return (StringConstant) constants.computeIfAbsent(value, StringConstant::new); | |||
} | |||
|
|||
public ByteConstant byteConstant(Byte value) { | |||
return (ByteConstant) funnyHashCodeConstants.computeIfAbsent(new ByteConstant(value), Function.identity()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we try to find a better name for funnyHashCodeConstants
? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally merge constants
and funnyHashCodeConstants
together (using the funnyHashCodeConstants
approach), not sure why they're separate (except perhaps a little less allocations in the constants
case?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also extend the BoxUnboxTest
now...
Let me take a look. |
a8913c7
to
8aae32a
Compare
Done. I also adjusted |
src/main/java/io/quarkus/gizmo2/impl/constant/ConstantImpl.java
Outdated
Show resolved
Hide resolved
We need constants of types `byte`, `short` and `char` because without them, we cannot statically make a difference between them and `int`, which may lead to all kinds of issues. For example, we could emit an invocation of a different overload of a method than expected, if the target class has an overload of the method for both `byte` (or `short` or `char`) and `int`. In addition to that, this commit also adds `Constant.of(Boolean)`, because `java.lang.Boolean` constants were not of the primitive type, unlike all others. This might be a bad idea, but all other constants of wrapper types are in fact constants of the corresponding primitive type, and consistency is warranted. We might later want to rethink that.
8aae32a
to
879cafa
Compare
Awesome, thanks! |
We need constants of types
byte
,short
andchar
because without them, we cannot statically make a difference between them andint
, which may lead to all kinds of issues. For example, we could emit an invocation of a different overload of a method than expected, if the target class has an overload of the method for bothbyte
(orshort
orchar
) andint
.In addition to that, this commit also adds
Constant.of(Boolean)
, becausejava.lang.Boolean
constants were not of the primitive type, unlike all others. This might be a bad idea, but all other constants of wrapper types are in fact constants of the corresponding primitive type, and consistency is warranted. We might later want to rethink that.