In the context of Java's Project Valhalla, how can I generically initialize a value type with its default value?
Initially, I thought that assigning null
to any value type would perform this initialization. However, the answers and comments to this question clearly show that null
is a reference, so it has nothing to do with value types (precisely due to the fact that value types are not references, but direct values instead).
E.g. if I have a Person
value type with StringValueType name
and DateValueType dateOfBirth
attributes (here dateOfBirth
would be a nested value type containing int year
, int month
and int day
attributes), how could I initialize my Person
value type in a generic way so that the values of its attributes are ""
for name
and (0, 0, 0)
(or the corresponding default value) for dateOfBirth
, respectively?
To make it more clear, if this were C, I would do:
memset(myPersonStructVariable, 0, sizeof(Person));
Or in modern C:
struct Person myPersonStructVariable = {0};
The value type equivalent of the aconst_null
bytecode (i.e. null
, which would be the default for reference types), is the vdefault
bytecode. From the minimal value type spec:
Operation
Push the default value of a direct value class type
Format
vdefault indexbyte1 indexbyte2
...
Description
The unsigned indexbyte1 and indexbyte2 are used to construct an index into the run-time constant pool of the current class (2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The run-time constant pool item at that index must be a symbolic reference to a direct value class type (4.4.1). The type is resolved (5.4.3.1).
The named class is initialized (5.5) if that class has not already been initialized. The default value (2.3.5) of the direct value class type is pushed onto the operand stack.
And 2.3.5:
2.3.5 Direct Value Class Types
...
The default value of a direct value class type is a class instance whose fields store the default values of their respective types. There is no special null value of a direct value class type.
And a quote from this presentation says:
[A default value] which is really a value of the right width, with all 0s inside.
So that would be similar to what you do in C with memset(myStructVar, 0, size)
.
There is currently no language support for value types, so we can not say if there will be something like a null
literal that would return the default value of a value type (e.g. MyValueType x = default(MyValueType)
or something like that), but the byte code exists. The presentation also shows how you'd use method handles to invoke vdefault
. Alternatively, you'd have to spin bytecode.
Initializing fields to a user defined value (e.g. ""
for StrinValueType
) would probably just happen through calling a constructor (or a value type equivalent). But it really isn't clear at this point in time, so we can only speculate.
Also, check out the latest draft for the valhalla vm prototype here: http://mail.openjdk.java.net/pipermail/valhalla-dev/2017-December/003631.html