-
Notifications
You must be signed in to change notification settings - Fork 213
Require "final" when using primary constructor syntax in a view declaration #2546
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
Comments
Annoying, but probably the correct call. "Final by default" as our next big thing, anyone? |
I was thinking that we would include the The same line of thinking would allow, but not require, each member in a view class to have the modifier |
If we say that I'd disallow (Or, heck, make them default to Whether I'd want to make the |
But we could make the I think it's useful to consider the failure mode: When a developer forgets about this rule, or doesn't know the rule in the first place, the outcome is a compile-time error in the case where that developer makes an attempt to modify one of those immutable instance variables. This means that the feedback is immediate when it matters, and no bugs are introduced silently. This is much less error-prone than, say, a rule that allows an implicitly covariant type variable to be used in a non-covariant position in a member signature. ;-) I think it won't hurt to consistently make the |
I think it's too dangerous to make explicitly declared instance variables final in a class because it has a We could allow adding |
And, in practice, this doesn't seem to bother many people. I like brevity and I wish non-assignable had always been the default. But it isn't, and that ship has sailed. I think there's something to be said for being consistent and simple even if it means a little verbosity in some cases. If someone is reading a view type or enhanced enum for the first time, they already have to learn what the On the other hand, flipping the default for |
I don't have strong feelings about this for views (as opposed to structs). |
Being immutable is a pretty major point for I don't think structs are particularly special in that regard. They're just one step further removed from a plain I think we should keep requiring What I do want is special behavior for a primary constructor's implicit fields, which depends on what the underlying construct is. (And I would introduce So, all in all: I am fine with allowing views to not require |
Agreed 1000%.
That's an interesting way to split it. So basically, if you opt in to using the primary constructor sugar, we try to sweeten it as much as possible. I think that principle probably reasonable extends to also letting you write But if you write the fields explicitly, they have a uniform syntax across all constructs. |
I adjusted the labels: Primary constructors are not expected to be specific to extension-types, they will (if accepted into the language) be applicable to classes, mixin classes, enums, extension types, and perhaps more. |
I love the primary constructor syntax in the views proposal. I really want that syntax for classes too. Based on that, I think we should require a
final
modifier when defining a primary constructor on a view:I'm assuming we will eventually do primary constructors for classes. When we do, I believe fields should default to non-final. That's consistent with how both parameter declarations and field declarations work today. It's also consistent with the view proposal that requires you to write
final
if you declare the representation as an instance field inside the body.Requiring
final
is a tiny amount of verbosity and we get consistency and clarity in return. The verbosity doesn't bother me much because I don't anticipate a large number of users authoring view types. A much larger number of them will end up reading them when they go to definition. When they do, since the feature may be unfamiliar, I think it helps for the syntax to be clearer and seeing thefinal
there can help them understand that the underlying representation field can't be assigned by the view.The text was updated successfully, but these errors were encountered: