Skip to content

Use @transient as an alternative for @constructorOnly #13423

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

Closed
wants to merge 1 commit into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Aug 30, 2021

The motivation for the PR is to move away from @constructorOnly to @transient. The documentation for @transient should be like the following:

/** When used on a field or a `val` or `var` parameter, the annotation is
 *  equivalent to Java's `transient`. That is, it indicates that the field
 *  should not be serialized.
 *
 *  When used on a regular parameter, the annotation indicates that the
 *  parameter is for initialization only and will not be retained as a field
 *  in the class. If the parameter would have to be kept as a field, for
 *  instance because it was accessed from a class method, the compiler
 *  reports an error.
 */
@field @param
final class transient extends scala.annotation.StaticAnnotation

Why rename?

@constructorOnly is bulky and awkward to write with its camelCase. transient is short and to the point. It is also already known, so we generalize the previous meaning instead of inventing a new name.

@transient will be very important once we go to capture checking. Essentially the rule would be that any class parameter
not marked as @transient is counted as possibly captured by the class. So we need a short name for this.

But even without the capture checking use case @transient is important, since it can be used to statically prevent certain classes of space leaks. So it plays a role similar to @tailrec.

Is the Double Meaning a Problem?

I think not. transient is the opposite of persistent. It marks something to have a shorter lifetime than usual. So for parameters that means it's not persisted as a field of the class and for fields it means it's not persisted when serializing.
If anything the parameter meaning is the more common and natural one, since most people do not care about Java serialization.

Is the Renaming Burdensome?

I hope not. Sor far constructorOnly has been documented only directly in the class. A Google search throws up nothing else. The CB has not a single use of it. So I think we can safely introduce @transient and deprecate @constructorOnly. But we should do it quickly.

@odersky odersky modified the milestone: 3.2.0 Aug 30, 2021
@odersky odersky marked this pull request as draft August 30, 2021 15:12
@odersky odersky force-pushed the add-transient-params branch 2 times, most recently from a9f5588 to 6f1e211 Compare August 30, 2021 19:32
@Jasper-M
Copy link
Contributor

When used on a regular parameter, the annotation indicates that the
parameter is for initialization only and will not be retained as a field
in the class. If the parameter would have to be kept as a field, for
instance because it was accessed from a class method, the compiler
reports an error.

IMO it should be clearer from the doc that @transient only asserts that the compiler does what the developer expected, but doesn't influence the generated bytecode. Even for @tailrec that's sometimes still a source of confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants