Skip to content

Support shorthand class creation syntax #1002

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

Closed
agrosner opened this issue May 31, 2018 · 9 comments
Closed

Support shorthand class creation syntax #1002

agrosner opened this issue May 31, 2018 · 9 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@agrosner
Copy link

For classes with a single constructor that takes in a few values.

class User {
  final String firstName;
  final String lastName;

  User({@required this.firstName, @required this.lastName});
}

It would be much cleaner and concise to simply allow a "primary constructor":

class User({@required this.firstName, @required this.lastName}) {
  final String firstName;
  final String lastName;
}

Duplicating constructor and class declaration seems a little clunky. When we have more fields,
this duplication is further known and wish we could take it a step further to allow:

class User({@required final String firstName, @required final String lastName});

Less duplication in code will lead to nice details in our widgets for flutter like from this:

class AppDetails extends StatefulWidget {
  final String url;
  final String jobName;

  AppDetails({@required this.url, @required this.jobName});

  @override
  AppDetailsState createState()  => new AppDetailsState(this.url, initialJobName: jobName);
}

class AppDetailsState extends State<AppDetails> {
  String selectedJobName;
  final String jobUrl;

  AppDetailsState(this.jobUrl, {@required String initialJobName}) {
    this.selectedJobName = initialJobName;
  }

  // state implementation below

}

to this:

class AppDetails({@required final String url, @required final String jobName})
 extends StatefulWidget {
  @override
  AppDetailsState createState() => new AppDetailsState(this.url, initialJobName: jobName);
}

class AppDetailsState(final String jobUrl, {@required String initialJobName}) 
 extends State<AppDetails> {

  // state implementation below

from 17 lines of boilerplate to 8.

@leafpetersen
Copy link
Member

cc @munificent @lrhn

@lrhn
Copy link
Member

lrhn commented Jun 1, 2018

I'm unable to find other requests for the same thing (which surprises me, except that I can never find anything using GitHub search).

What we are asking for here is a shorter syntax to write a simple class, without having to repeat the field names (or even class name) in a constructor.

@zoechi
Copy link

zoechi commented Jun 6, 2018

@SAGARSURI
Copy link

Is there something similar to inline classes in Dart?

@eernstg
Copy link
Member

eernstg commented Jun 1, 2020

No, we don't have that currently. The main property of an inline class (corresponding to Haskell newtype) is that it is a zero cost wrapper, in the sense that it is a new type which is distinct from (and not assignable to) the type of the wrapped value, but the run-time representation is just that wrapped value. One example of work in this direction is https://github.com/dart-lang/language/blob/master/working/static%20wrapper%20types/feature-specification.md.

@SAGARSURI
Copy link

Thanks @eernstg for the info. Excited for the above feature to be added in Dart.

@eernstg
Copy link
Member

eernstg commented Jun 2, 2020

Moving this issue to the language repo, it would be a language mechanism.

@eernstg eernstg transferred this issue from dart-lang/sdk Jun 2, 2020
@kevmoo
Copy link
Member

kevmoo commented Jun 17, 2020

Seee #698 – covers your case nicely I think!

@lrhn lrhn added the feature Proposed language feature that solves one or more problems label Jul 14, 2020
@leafpetersen
Copy link
Member

We're tracking this here, closing in favor of that issue (let me know if I've missed something distinct about this request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

7 participants