-
Notifications
You must be signed in to change notification settings - Fork 228
Specify that non-plain primary parameters are immutable #4575
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
…ons (but a primary parameter can still be modified in the body of a primary constructor)
…ons (but a primary parameter can still be modified in the body of a primary constructor)
|
Do we want this? Example of something you could do using side-effects: class StreamWrapper<T>([StreamController<T>? existingController]) {
final Stream<T> stream;
final StreamController<T> controller;
init:
controller = existingController ??= StreamController(),
stream = controller.stream;
}Initializer list entries are (probably) run after all initializing expressions, so asssignments only affect themselves and the body block. It may be a little confusing if we have: class C(int x) {
final x1 = x;
this: x3 = ++x;
final x2 = x;
final int x3;
}because the three references to Then don't do that. Put the |
accepted/future-releases/primary-constructors/feature-specification.md
Outdated
Show resolved
Hide resolved
|
@lrhn wrote:
Oh, you're right, we did not include non-declaring, non-initializing-formal, non-super parameters (we could call them plain), they can still be mutated also in initialization code. I adjusted the wording to match the decision. |
…s only, as decided by the language team
This PR specifies that it is a compile-time error to assign a value to a formal parameter declared by a primary constructor in the initializer list of the body part of the primary constructor, as well as in the initializing expression of a non-late instance variable declaration.