-
Notifications
You must be signed in to change notification settings - Fork 125
Basic .new support for constructors #2766
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
Basic .new support for constructors #2766
Conversation
lib/src/model/class.dart
Outdated
/// With constructor tearoffs, this is no longer equivalent to the unnamed | ||
/// constructor and assumptions based on that are incorrect. Default | ||
/// constructors can be named, and there may not be an unnamed constructor | ||
/// if the default constructor is declared with `.new`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'm not sure I understand this. You cannot "declare" a default constructor. Does the analyzer create a synthetic one with the name, .new
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, analyzer's ConstructorElement has a isDefaultConstructor
which returns true if the element can be used as a default constructor. It makes sense in a client API though, because generally subclasses do not care if the unnamed, zero-arg constructor of a super class is default or not; they only care whether it exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little difficult to parse through what is merely the analyzer's API for accessing it and what someone looking at the spec would say, and the lay programmer, for sure. I do intend "default constructor" to mean "thing that will be used by default to construct this object". There seem however to be three general cases:
-
No constructor is declared, e.g.
class A {}
. In this case, I am intending for theA.new
name to be available to refer to the "unnamed" constructor, as well asA.A
, an alias that already exists. -
A constructor is declared as
A.A(param1, param2)
. Dartdoc calls this a "default constructor", and an unnamed constructor, for historical reasons, because the name of it is indistinguishable from case 1 and back when this code was written there seemed to be no way to definitively disambiguate the two situations in all cases. -
A constructor is declared as
A.new(param1, ...)
. Dartdoc will also call this "default constructor", but no longer an unnamed constructor. Presumably it is illegal to have case 2 and case 3 at the same time, haven't actually checked :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A constructor is declared as
A.A(param1, param2)
.
Do you mean A(param1, param2)
? Declaring a constructor as A.A()
is not an unnamed constructor, and cannot be invoked via A()
.
A constructor is declared as
A.new(param1, ...)
. Dartdoc will also call this "default constructor", but no longer an unnamed constructor.
Why no longer unnamed? If you declare a constructor with A.new(param1, ...)
, it is (according to the spec, and analyzer) an unnamed constructor, and can be invoked with A(...)
. Is this saying that a constructor declared with A.new(param1, ...)
cannot be referred to in docs via [new A()]
?
I'd actually be surprised that you could discover that one was declared with A.new
because I think I ensured that the analyzer always considers that constructor as named ''
(unnamed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TL;DR is that dartdoc has been wrong about how default constructors work, but not in a way that has actually caused any real world problems and probably would continue to have been oblivious were it not for tearoffs. I'll try to rationalize this now.
…to constructor-tearoffs-newimpl
Continuing work on #2655.
This is a little squirrelly, since we allow referring to default constructors via old and new methods. Also, not working with typedefs yet.