Skip to content
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

Do we allow a late final variable to obtain an initializing expression from an augmenting declaration? #3735

Open
eernstg opened this issue Apr 29, 2024 · 2 comments
Labels
augmentation-libraries question Further information is requested

Comments

@eernstg
Copy link
Member

eernstg commented Apr 29, 2024

Consider the following program:

// --- Library augmentation 'augment.dart'.
augment library 'lib.dart';

augment late final int x = 14;

// --- Library 'lib.dart'.
import augment 'augment.dart';

late final int x;

I can't find a rule to say that this is a compile-time error in the feature specification.

However, I do think it should be an error: The augmenting declaration (presumably) adds an initializing expression to the declaration of x in lib.dart, but at the same time it eliminates the implicitly induced setter (which is needed because we must be able to initialize the variable, but it has a restricted semantics because we are not allowed to mutate the value, that is, it will throw on every non-first invocation).

I think it is a unique property of this kind of augmentation that it eliminates a setter which would otherwise exist (I don't think we have any other augmentations that will eliminate any entity).

(I think we are just about to decide that library augmentations should just be part files, but that shouldn't change anything.)

@dart-lang/language-team, WDYT?

@jakemac53
Copy link
Contributor

Interesting - yes it does sound like a unique property of this kind of augmentation, which is not desirable.

I think it would be reasonable to say that an augmentation is not allowed to introduce an initializer for a late final variable.

This can't be expressed through the macro API unfortunately, but we can just rely on the general purpose error (to be specified in the augmentation library spec).

@lrhn
Copy link
Member

lrhn commented May 1, 2024

From the general rule of augmentations not changing signatures, an augmentation should not be allowed to add an initializer to a late final variable that has none.
A late final with no initializer has a setter, and a late final with an initializer does not, so adding an initializer should significantly change the signature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
augmentation-libraries question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants