Comments (4)
If you make foo
and bar
constants, you will have what you want.
A final
value is not constant, and it can be changed, although it can't be reassigned. This is why the compiler can't generally assume the value.
In your specific case maybe it could, as we have a final
field of a private class, so we know it's never overriden with another getter or something like this. But it's easier for you to make it const
than we deal with such specific cases.
from language.
@mateusfccp my _Item
class has functions and nullable fields though. It looks like this:
final class _Item{
final String value;
final Function fn;
String? content;
_BackupItem({required this.value, required this.fn});
}
I don't think I can make this const, right? That's why I'd like the compiler to realize that my single field is const. It has no problem realizing that something like
final x = 10;
is constant and lets me use it in a switch statement.
But it's easier for you to make it const than we deal with such specific cases.
Is the logic for recognizing const fields not already there? Wouldn't that be useful in any case for optimization purposes like constant propagation etc? I'd like to understand what the factors are when thinking about a feature like this.
from language.
Switch cases must be constant, because the compiler needs to know the values to determine if the switch is exhaustive.
Even for switches that don't have to be exhaustive, the value needs to be constant so that the compiler can optimize the switch based on known value. A non-constant value isn't known to the compiler. The foo.value
is not constant because the object referenced by foo
doesn't exist yet at compile-time. It may never exist, it's only created the first time the foo
variable is read.
So it isn't "statically known" at all.
Also, even if the foo
object could be made to be known at compile-time, somehow, then its value
getter cannot be assumed to be stable. Reading it more than once must be assumed to be able to give different values.
It can't in the current program, but the _Item
class doesn't promise to not change value
to a getter in a later version. If a switch can assume that the variable is stable, then it becomes a breaking change to change it to a getter, something that's not breaking today.
In very, very many cases where it looks "obvious" that a program has some statically determinable property, and the compiler doesn't use that, it's because using that property for anything makes it breaking to change the code. Any time the compiler assumes, fx, that a final
field is stable, it becomes breaking to change that final
field to a getter. Even if the author never intended or promised that the variable would be stable, or that its value could be known at compile-time.
That's why the compiler requires you to write const
in front of constant variables, even if it's "obvious" that the value is constant. It might not be the author's intent, it's just an accident of implementation, and if the compiler changes behavior based on something the author hasn't promised, it makes more things breaking changes.
from language.
@lrhn Thanks for the insightful reply. I appreciate that you took the time to explain that.
from language.
Related Issues (20)
- More flexibility for the `base` keyword
- Consider moving _macros/macros into _fe_analyzer_shared for breaking changes HOT 11
- Static type metadata guided shorthands and features HOT 4
- No macro.TypeDeclarationImpl for `dynamic` HOT 4
- Adoption of functional programming in the Dart language HOT 2
- Enhancing `library` and `part of` Declarations in Dart HOT 1
- Create the type `Monad` HOT 4
- Allow `import` and `part` to have wildcard path to import every lib in specific directory HOT 1
- Adding conditional imports/parts with macros. HOT 2
- Consider changing the context for the operand of `throw` to `Object`. HOT 3
- could support static extension? HOT 2
- Macro - Augment class with generic HOT 7
- Macro annotation information in yaml files next steps HOT 2
- Network paths don't work anymore with version 3.4.0 HOT 2
- "Merge to source" for macros that only need Phase 3 HOT 19
- Follow up on "spawn isolate from kernel blob" implementation
- Allow pattern `case` conditions in `while` statements. HOT 2
- Unable to call super method on an object. HOT 2
- Add syntactic sugar for package imports HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language.