如何使用默认值通用地初始化值类型? [英] How to generically initialize a value type with its default value?

查看:190
本文介绍了如何使用默认值通用地初始化值类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java的 Project Valhalla 的背景下,我如何通常使用默认值初始化值类型吗?

最初,我认为将null分配给任何值类型都会执行此初始化.但是,对此问题的答案和评论清楚地表明null引用,因此它与值类型无关(正是由于值类型不是引用,而是直接值).

例如如果我具有带有StringValueType nameDateValueType dateOfBirth属性的Person值类型(此处dateOfBirth将是包含int yearint monthint day属性的嵌套值类型),如何初始化Person以通用方式输入值,以便其属性值分别对于namedateOfBirth分别为""dateOfBirth(或相应的默认值)? >

为了更清楚一点,如果是C,我会这样做:

memset(myPersonStructVariable, 0, sizeof(Person));

或者在现代C语言中:

struct Person myPersonStructVariable = {0};

解决方案

aconst_null字节码等效的值类型(即null,这将是引用类型的默认值)是vdefault字节码.根据最小值类型的说明:

6.5 vdefault

操作

推送直接值类类型的默认值

格式

vdefault indexbyte1 indexbyte2

...

说明

无符号的indexbyte1和indexbyte2用于构造索引 放入当前类(2.6)的运行时常量池中,其中 索引的值是(indexbyte1<< 8)|索引字节2.运行时 该索引处的常量池项必须是对a的符号引用 直接值类别类型(4.4.1).类型已解决(5.4.3.1).

如果命名的类尚未初始化,则将其初始化(5.5) 已初始化.直接值类别的默认值(2.3.5) 类型被压入操作数堆栈.

和2.3.5:

2.3.5直接值类类型

...

直接值类类型的默认值是类实例 其字段存储各自类型的默认值.那里 不是直接值类类型的特殊空值.

此演示文稿的引用说:

[默认值]实际上是正确宽度的值,其中全为0.

因此,这与使用memset(myStructVar, 0, size)在C语言中执行的操作类似.


当前不支持值类型的语言,因此我们无法确定是否会出现诸如null文字之类的东西,它们会返回值类型的默认值(例如MyValueType x = default(MyValueType)或类似的东西),但是字节码存在.该演示文稿还显示了如何使用方法句柄调用vdefault.另外,您还必须旋转字节码.

将字段初始化为用户定义的值(例如"""")可能仅通过调用构造函数(或等效的值类型)来完成.但是目前还不清楚,所以我们只能推测.


此外,在此处查看valhalla vm原型的最新草案: http://mail.openjdk.java.net/pipermail/valhalla-dev/2017-December/003631.html

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:

6.5 vdefault

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

这篇关于如何使用默认值通用地初始化值类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆