Skip to content
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

named arguments to set fields in a constructor #155

Closed
sigmundch opened this issue Oct 16, 2015 · 3 comments · Fixed by #442
Closed

named arguments to set fields in a constructor #155

sigmundch opened this issue Oct 16, 2015 · 3 comments · Fixed by #442

Comments

@sigmundch
Copy link
Contributor

I am not sure if this feature request is worth it, but I wanted to share the idea, so here it is:

What if we take generated messages and make a constructor that takes as arguments the actual fields from the proto buff? for example:

message A {
  required someField = 1;
  optional otherField = 2;
}

generates:

class A extends GeneratedMessage {
  A.filled(this.someField, {this.otherField});
}

I just often see patterns where developers have all the data they need to fill in, and end up writing code like:

  new A()
      ..someField = a
      ..otherField  = b

instead of:

  new A(a, otherField: b)
@skybrian
Copy link
Contributor

I've been thinking of doing this along with support for read-only protobufs. I'm not working on it, though.

(I think it would be better to use named parameters for all fields. Protobuf fields usually don't have a logical order.)

@skybrian skybrian changed the title consider generating constructors? named arguments to set fields in a constructor Nov 15, 2016
@gamebox
Copy link

gamebox commented Mar 21, 2018

I'd like to take this on, if you are interested in contributions. I've already signed the Google contributor agreement.

@mit-mit mit-mit transferred this issue from dart-archive/dart-protoc-plugin Dec 14, 2018
@aloisdeniel
Copy link

aloisdeniel commented Jun 20, 2019

I agree !

This would be a lot easier to write embedded affectation IMO. Only using named parameters could be even better too (like Flutter API). You can make named parameter @required with meta package.

I find that this kind of instantiation is quite a mess (too many parenthesis and dots) :

Person()
      ..name = "John"
      ..address = (Address()
         ..street = 5
         ..label = "Baker street"
         ..country = (Country()
             ..code = "en"
             ..name = "england"
        )
    )

The following feels a lot more natural to me, and more aligned with traditional dart APIs :

Person(
  name: "John",
  address: Address(
      street: 5,
      label: "Baker street",
      country: Country(code: "en", name: "england")
  )
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants