What are Value Types from Project Valhalla?

后端 未结 4 619
说谎
说谎 2021-02-02 08:07

I\'ve started reading about Project Valhalla and there is something I really don\'t understand and it\'s the Value Types.

This is what I understand:

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 08:47

    UPDATE 2019/2020, Build 14-valhalla+4-55 (2019/8/30)

    Inline Class is the new name of "Value Type". According to current project state Inline Classes don't support inheritance but are able to extend interfaces. Following statement is legal:

    public inline class MyInlineClass extends SomeJavaInterface {}
    

    They do have equals, hashcode (and toString) methods. "==" operation and equals (default implementation) are same for simple case, where only Inline Classes and primitive fields are involved. But it is not true in general.

    It becomes messy, if you add a classic Java Object (including String and Array) as a field inside of Inline Class. In this case "==" operation most probably won't work as expected anymore and you have to fallback to classic custom equal implementation.

    Inline objects are immutable and don't support clone (clone makes no sense).


    A possible usecase for Inline Classes would be complex numbers or matrix algebra, which is essential for neural networks algorithms of artificial intelligence systems.

提交回复
热议问题