Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
lidaisy wants to merge2 commits intoscala:main
base:main
Choose a base branch
Loading
fromlidaisy:valhalla-value-classes

Conversation

@lidaisy
Copy link
Contributor

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 perJEP 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 (seeJEP 401). When used with the Project Valhalla JVM, Valhalla value classes are optimized.

Valhalla value classes extend AnyVal and have avalhalla annotation. 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.

importscala.annotation.valhalla@valhallaclassValhallaValueClass(valx:Int,valy:Int)extendsAnyVal

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 extendAnyVal, universal traits, or abstract valhalla value classes.

Valhalla Traits

Valhalla Traits are Universal Traits (traits that extend Any) with avalhalla annotation.

Like Valhalla value classes, any Valhalla trait must have immutable fields only.

Valhalla traits can extendAny or universal traits.

importscala.annotation.valhalla@valhallatrait(valx:Int,valy:Int)ValhallaTraitextendsAny

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 ofnull and a valhalla value class returnstrue.

He-Pin and lenguyenthanh reacted with hooray emojiHe-Pin, windymelt, and lenguyenthanh reacted with heart emojiHarrisL2 reacted with rocket emoji
```scala
import scala.annotation.valhalla

@valhalla trait(val x: Int, val y: Int) ValhallaTrait extends Any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
finalclassvalhallaextendsStaticAnnotation
@scala.annotation.experimental
finalclassvalhallaextendsStaticAnnotation

Copy link
Member

@bishaboshabishaboshaDec 9, 2025
edited
Loading

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?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The errors should not be there. Value classes and traits should be allowed to extend non-valhalla Universal Traits. I have updated the file accordingly.

* class as specified in https://openjdk.org/jeps/401.
*/

final class valhalla extends StaticAnnotation
Copy link
Contributor

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

What aboutvalue class ConcreteVVC...

987Nabil reacted with thumbs up emoji
@natsukagaminatsukagami self-requested a reviewDecember 18, 2025 14:47
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@bishaboshabishaboshabishabosha left review comments

@natsukagaminatsukagamiAwaiting requested review from natsukagami

+1 more reviewer

@He-PinHe-PinHe-Pin left review comments

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@lidaisy@He-Pin@bishabosha

[8]ページ先頭

©2009-2025 Movatter.jp