-
Notifications
You must be signed in to change notification settings - Fork 1.1k
JEP 401: Project Valhalla value classes #24705
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
base: main
Are you sure you want to change the base?
Conversation
f9ca8e3 to
a7db0cd
Compare
| ```scala | ||
| import scala.annotation.valhalla | ||
|
|
||
| @valhalla trait(val x: Int, val y: Int) ValhallaTrait extends Any |
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.
| @valhalla trait(val x: Int, val y: Int) ValhallaTrait extends Any | |
| @valhalla trait ValhallaTrait(val x: Int, val y: Int) extends Any |
| * class as specified in https://openjdk.org/jeps/401. | ||
| */ | ||
|
|
||
| final class valhalla extends StaticAnnotation |
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.
| final class valhalla extends StaticAnnotation | |
| @scala.annotation.experimental | |
| final class valhalla extends StaticAnnotation |
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.
this file seems be recording error cases, but is in the pos suite, does that mean the compiler is not enforcing the requirement, or was it a now-invalid requirement?
| * class as specified in https://openjdk.org/jeps/401. | ||
| */ | ||
|
|
||
| final class valhalla extends StaticAnnotation |
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 think this should be a value or value class instead of @valhalla
| import scala.annotation.valhalla | ||
|
|
||
| @valhalla | ||
| class ConcreteVVC(val a: Int, val b: Int) extends AnyVal { |
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.
What about value class ConcreteVVC...
I implemented Project Valhalla's value classes. The documentations are in docs/_docs/reference/experimental/valhalla.md and I have copied it below as well.
As per JEP 401, there is a new attribute LoadableDescriptors in the class files. Thus, this PR depends on modifications in the scala-asm repo. Here is my fork of it for anyone who wants to try it out.
You should use the flag --Yvalue-classes to compile any value classes, since this changes the class file's major and minor version to be compatible with Project Valhalla's JVM.
Documentation
Valhalla Value Classes
Valhalla value classes are the Scala equivalence of Java's Project Valhalla's value classes (see JEP 401). When used with the Project Valhalla JVM, Valhalla value classes are optimized.
Valhalla value classes extend AnyVal and have a
valhallaannotation. Valhalla value classes cannot have non-parameter fields and cannot have auxilliary constructors.Valhalla value classes do not have object identity -- two valhalla value classes are equal when their fields are the same.
Valhalla value classes are implicitly final and cannot be extended unless it is abstract. Its fields are immutable and cannot be lazy.
Valhalla value classes can extend
AnyVal, universal traits, or abstract valhalla value classes.Valhalla Traits
Valhalla Traits are Universal Traits (traits that extend Any) with a
valhallaannotation.Like Valhalla value classes, any Valhalla trait must have immutable fields only.
Valhalla traits can extend
Anyor universal traits.Using Explicit Self with Valhalla
Valhalla traits can have self-type of any trait without mutable fields.
CanEqual with Valhalla
Valhalla value classes can be null, so the CanEqual of
nulland a valhalla value class returnstrue.