Skip to content

Would Replacing LateInitializationError with LateInitializationException Be More Intuitive? #4029

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
Eglusaxie opened this issue Aug 13, 2024 · 3 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@Eglusaxie
Copy link

class Example {
late final String x;
}

main() {
Example example = Example();
try {
print(example);
} on LateInitializationError catch (e) {
x = "late init";
print(x);
}
}
This statement isn't working at the moment, but I feel like it should be allowed.

@Eglusaxie Eglusaxie added the feature Proposed language feature that solves one or more problems label Aug 13, 2024
@julemand101
Copy link

Should not be allowed. It is an Error since they follow the definition of Error:

Error objects thrown in the case of a program failure.

An Error object represents a program failure that the programmer should have avoided.

Examples include calling a function with invalid arguments, or even with the wrong number of arguments, or calling it at a time when it is not allowed.

These are not errors that a caller should expect or catch — if they occur, the program is erroneous, and terminating the program may be the safest response.

When specifying late, you promise the compiler that you know what you are doing as a programmer, and the field will ALWAYS have a value when first time access the field. If you can't promise that, then your code are erroneous and should be terminated.

Exceptions are for situations which you can't predict like e.g. disk are full, bad network and so on. It should never be unexpected if a late field are being given a value or not.

If your program are unpredictable when it comes to fields being set or not, then those fields must be defined as nullable so you, as a programmer, are forced to check if the field contains a value before using it. This pattern are much more safe and prevents the errors you are trying to handle in your example.

@Eglusaxie
Copy link
Author

Should not be allowed. It is an Error since they follow the definition of Error:

Error objects thrown in the case of a program failure.
An Error object represents a program failure that the programmer should have avoided.
Examples include calling a function with invalid arguments, or even with the wrong number of arguments, or calling it at a time when it is not allowed.
These are not errors that a caller should expect or catch — if they occur, the program is erroneous, and terminating the program may be the safest response.

When specifying late, you promise the compiler that you know what you are doing as a programmer, and the field will ALWAYS have a value when first time access the field. If you can't promise that, then your code are erroneous and should be terminated.

Exceptions are for situations which you can't predict like e.g. disk are full, bad network and so on. It should never be unexpected if a late field are being given a value or not.

If your program are unpredictable when it comes to fields being set or not, then those fields must be defined as nullable so you, as a programmer, are forced to check if the field contains a value before using it. This pattern are much more safe and prevents the errors you are trying to handle in your example.

Yes, now that I think about it, you're absolutely right.

@mateusfccp
Copy link
Contributor

Not exactly what you are proposing, but #3680 would allow us to query for late variable initialization.

I think this would enable you to do what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants