Suppose I have the following code, where `x` is an...
# language-proposals
d
Suppose I have the following code, where
x
is an `Int`:
Copy code
val y = x.takeIf { it > 5 } ?: 3
The compiled bytecode becomes:
Copy code
Integer var2 = x;
int it = ((Number)var2).intValue();
int y = (it > 5 ? var2 : null) != null ? it > 5 ? var2 : null : 3;
This has some undesirable boxing and unboxing, it would be ideal if the compiler could optimize this to the equivalent of:
Copy code
val y = if (x > 5) x else 3
which compiles in Java to:
Copy code
int y = x > 5 ? x : 3;
What makes this particularly desirable? Consider the protobuf pattern: https://developers.google.com/protocol-buffers/docs/javatutorial#the-protocol-buffer-api
Copy code
public boolean hasId();
public int getId();
To avoid boxing, primitives are stored with a default value, and a bitfield to indicate which primitives are truly set. Otherwise, we would ideally have a single method:
Copy code
inline fun getId(): Int? = if (hasId()) getPrimitiveId() else null
It is inlined so that any boxing elimination could be carried out across method calls. If the compiler could eliminate unnecessary boxing, then this call:
Copy code
if (proto.hasId()) proto.getId() else calculateDefault()
could be replaced with the more straightforward:
Copy code
proto.getId()?: calculateDefault()
👍 4
f
Protobuf never gives you
null
and has no
has
method for scalar types (they had in Proto2 if I recall correctly). That said, you're request makes perfect sense. I think there is a lot of room to improve the compiler output.
d
This is a use case in which, if boxing could be optimized out, Proto would be able to use
null
in place of requiring
has
followed by
get
. From my link, you can see the
hasId
method, what does would it have in Proto3?
f
In Proto3 scalar values always have a default and
optional
and
required
do not exist anymore. An
int32
always gives you
0
if it was not set. You have to use a message if you want to know if it is present or not.