Skip to content

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

Merged
merged 11 commits into from
Aug 30, 2021

Conversation

jcollins-g
Copy link
Contributor

@jcollins-g jcollins-g commented Aug 26, 2021

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.

@google-cla google-cla bot added the cla: yes Google CLA check succeeded. label Aug 26, 2021
@jcollins-g jcollins-g requested a review from srawlins August 26, 2021 22:03
/// 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`.
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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:

  1. No constructor is declared, e.g. class A {}. In this case, I am intending for the A.new name to be available to refer to the "unnamed" constructor, as well as A.A, an alias that already exists.

  2. 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.

  3. 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 :-)

Copy link
Member

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).

Copy link
Contributor Author

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.

@jcollins-g jcollins-g merged commit c851212 into dart-lang:master Aug 30, 2021
@jcollins-g jcollins-g deleted the constructor-tearoffs-newimpl branch August 30, 2021 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Google CLA check succeeded.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants