-
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
Be able to closurize constructors #10659
Comments
new Foo The obvious syntax for this to me is to just look like a constructor invocation without any (). That's what closurized method calls look like. So: new Foo(1, 2, 3); would do the same as: var closure = new Foo; |
This comment was originally written by @seaneagan I like it! Only down side is that "new " is redundant for named constructors. Consider: new Foo vs Foo.new |
This comment was originally written by @stevenroose What's the chance that this will make it into the codebase anytime soon? I'm working on a library that may benefit from closurizable constructors. So it this change will eventually come through, I would pause a portion of the implementation and focus on another part first. If it doesn't, I'll need to find an alternative solution. |
It's not likely to be very soon, since we are trying to keep things stable. I believe it will come through, but it may be quite a long time. |
This comment was originally written by @stevenroose Will this change cause instability then? It shouldn't break anything. |
This comment was originally written by @seaneagan My use case: In unscripted, I would like to uniformly use closures to model command-line script behavior. The closures are annotated with command-line metadata, but in the case of sub-commands the methods are usually constructors. So my users would need to create a dummy closurizable factory method which delegates to the constructor and has the metadata. I imagine there are other scenarios where the metadata would actually have to be duplicated on manually created constructor closures, along with the entire constructor signature (including the return type, i.e. the class being constructed), and refactoring tools would be of no help when the constructor changes. |
This comment was originally written by @seaneagan I saw a closurization operator [1] was added to ClassMirror, but it appears to not support constructors, and doesn't specify whether the returned closure's function [2] is the actual method declaration which would need to be the case in order to get to the metadata. |
This comment was originally written by @seaneagan It's probably obvious, but constructor closures should be canonicalized constants, just like any other static method closures. That would be useful for using them as default factory methods, since default values must be constants. |
This comment was originally written by @seaneagan It would be nice if this could support type arguments: var fooFactory = new Foo<int>; |
Removed Type-Defect label. |
Issue #17907 has been merged into this issue. |
Just a note that we are looking at this again. |
This comment was originally written by @stevenroose Nice to hear that! I have ((x) => new X(x)) code blocks everywhere. |
This comment was originally written by @kaendfinger See @gbracha's proposal here: https://github.com/gbracha/generalizedTearOffs/blob/master/proposal.md |
I guess https://github.com/gbracha/metaclasses/blob/master/proposal.md is supposed to lead to another solution (as reference for people finding this issue first). |
I think this can be closed. void main() {
var constructor = new SomeClass#someConstructor;
var x = constructor();
print(dateConstructor(isUtc: true)(2000,1,1));
print(dateConstructor(isUtc: false)(2000,1,1));
}
Function dateConstructor({bool isUtc}) {
if(isUtc) {
return new DateTime#utc;
} else {
return new DateTime#;
}
}
class SomeClass {
SomeClass.someConstructor() {
print('constructing...');
}
} runs fine, just the analyzer still shows warnings for output:
|
This doesn't work anymore. At least not in the dartpad and on my project. |
@rrousselGit that feature was removed a short time after it was added. They'll try to find a better approach sometimes after Dart 2.0 |
Now that Since |
Any plan on this now that dart 2.0 is out for some time? |
Following that logic, |
I have a plan! It's still mostly in my head, but it exists.
I hope we can use the context type to figure out whether something like |
Just want to throw in my $.02: Is there any particular reason why if (isUtc) {
d = Date(...);
} else {
d = Date.utc(...);
}
// becomes
final ctor = isUtc ? Date : Date.utc;
d = ctor(...); I'm not at all familiar with how the Dart compiler works internally, but from what I know of compilers in general it's definitely possible to say, "Hey, that typename is being used as an expression, so it has to mean the default constructor". Am I missing an edge case where there is ambiguity? |
It's unlikely that In Dart 2, class instances are not functions. To make I think it's more promising to let the meaning of Function ctor = isUtc ? Date : Date.utc;` to give the type hint needed to distinguish |
@lrhn Whoops, I forgot that the type is, itself, an object (i.e. |
Over the past year working with Flutter. I've found several times I would like to refer to a constructor function. What was the problem with the old |
You can always create a closure for that |
That's one of the reasons why I think spread operator for classes would be lovely.
|
I assume tear-offs for constructors and getters/setters are still planned. |
I think we should also consider the utility in functions like Column(
children: textArray.map((s) => FancyText(s))
) With this, you could write Column(
children: textArray.map(FancyText.new)
) That's obviously a trivial example, but it is a (very nearly) real one that I recently had to use, and in my experience with other languages -- typically more functional ones, though not exclusively -- it's very common. Letting ctors be called like any other function taking arguments and returning a |
In similar situations we've moved an SDK issue to the language repository, but in this case dart-lang/language#216 contains a more recent version of the same proposal, so I'll close this one. Please go there for additional discussion! |
It would be nice to be able to closurize constructors. I find myself writing a de-serialization for DateTime that needs to be of the form
It's not that big a problem, but it would be nice to be able to write the parameter list once. There are some obvious questions. How would you refer to the default constructor? Could you provide type arguments? Is it an issue that invoking a closure might implicitly be doing a "new"?
The text was updated successfully, but these errors were encountered: