-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make const / new optional #18241
Comments
Removed Type-Defect label. |
This comment was originally written by @seaneagan To expand on const methods, in addition to regular methods, they could also support: // getters // final instance fields (implied instance getters) // operators This would remove all the special cases from the language spec about what can be considered a constant, and instead that can be seen directly in the api docs of dart:core, and any other lib which wants to define const methods. |
This comment was originally written by grant.js...@gmail.com Would it not be great if, instead of using: |
This comment was originally written by @zoechi Thats how is is currently. |
Const was very carefully limited in Darr,by design. We chose not to introduce const methods/getters etc. with notions of constness propagating. This is always subtle (well, a rat hole really). The cases where things are constant are explicitly defined in the spec and we want it that way. |
This comment was originally written by @seaneagan Hi Gilad, The limitedness of the const feature in dart currently limits the ability to use (and predict when one will be able to use) the dependent language features of metadata, default values, and canonicalization. This issue proposes two simple, and backward compatible extensions that together pretty much completely solve this problem in my opinion:
Can you please address both of these extensions separately, and also why you consider them a rat hole, i.e. what cases do they leave unaddressed? Why is restricting the ability to define constants a goal? Thanks! |
This comment was originally written by @zoechi To 1) I wonder if it's possible in this case to see (as a developer) when const is used. What when I know something should be const but something not obvious at callers site prevents it? When I can't explicitly declare it const I can't get a warning. When something can be const but I don't want it to be const because I want to change it later, how can I prevent that? Just because something can be used in a const context doesn't mean I always want to. |
This comment was originally written by @seaneagan Yes, so when you declare something to be const (currently only constructors |
I think there already is a separate issue for (1). As for extending the number of constructs that can be marked const (item 2). It is a slippery slope. The demands never end. Can I make an instance method constant, and if not, why not (we already had this debate for instance variables)? And now we need to execute arbitrary code at compile time. What if it contains loops? Just restrict them to expressions? Can they be recursive? Directly or indirectly? Can they use conditionals? For every limitation, someone comes up with a use case, an example that looks plausible in isolation. There are answers to all these things, but complexity is a bad thing. Ultimately we make a judgement call as to how far we should go and which directions are worth investing in. |
Are you thinking of issue #5680 ? That's to remove |
This comment was originally written by @seaneagan Great questions:
Yes, const instance method calls would only be considered const when the receiver and all arguments are const int x; const Foo(this.x); Notice that both the instance variable and method must be explicitly marked
Yes, just expressions. That would solve any use case I've ever had.
I guess if you do allow recursion, then you'd need to detect infinite recursion during compilation by setting a max stack depth. I'm curious if anywhere else in dart already requires this. |
I rest my case. You should be able to see how far from trivial or straightforward the issues are, and how vast the gap is between showing an example and actually working out what the rules are and implementing it. This clearly qualifies as a rathole. |
const may be a rathole but can we at least make |
"optional new" was http://dartbug.com/18241 - it's closed as WontFix. |
That's this same issue. But it includes making |
This comment was originally written by @richardeakin re I think Gilad is clearly not interested, as it has been debated to quite some extent, though I would urge the dart team to reconsider based on popular voice. I for one think that many expressions would be much clearer if the weren't plagued with new statements for simple objects, but I come from a C++ background where new is rarely used. const, well I like const too but that is a different topic altogether! It should be discussed separately. |
I created a new discussion for |
This issue was originally filed by @seaneagan
issue #4046 is about making the "const" / "new" modifier optional within const expressions, defaulting it to "const".
This could be generalized to make "const" / "new" optional everywhere, for both consistency and to add the following capabilities:
In const constructor initializers, leaving off "const" would allow the constness to be determined by the constructor call site, which would mean you could call other const constructors for example.
It would allow for the existence of const functions, and const factory constructors. And finally, leaving off "const" / "new" from any other call sites, would default it to "new", which would make it no longer necessary to expose top-level functions (extra API surface, lack of generic type support) whose sole purpose is to allow the call site to omit "new " when creating an instance
Example:
class Foo {
final int x;
const Foo(this.x);
const factory Foo.bar(x) => Bar(x, []);
}
class Bar extends Foo {
final List list;
const Bar(int x, this.list) : super(x);
}
const foo(x) => Foo(x);
Then the following equivalencies would hold:
a) const foo(x) -> const Foo(x)
b) foo(x) -> new Foo(x)
c) const Foo(x) -> const Bar(x, const [])
d) new Foo(x) -> new Bar(x, [])
e) Foo(x) -> foo(x)
Examples const functions would be the top-level functions in dart:math. And package:matchers could expose matchers as const constructors instead of top-level methods, which would allow them to be used in both annotations, and test code, without needing "new" / "const" in either case, and the matchers would then also be able to support generic type arguments. See:
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/0_DXEjQSoiU
The text was updated successfully, but these errors were encountered: