Skip to content

[pigeon] Respect original constructors #160830

@Levi-Lesches

Description

@Levi-Lesches

Use case

Currently, Pigeon generates constructors with named parameters for all fields, even if a different constructor is provided. For example:

class AndroidColor {
  AndroidColor.fromARGB(this.a, this.r, this.g, this.b);

  final int a;
  final int r;
  final int g;
  final int b;
}

generates

/// A color on Android.
class AndroidColor {
  AndroidColor({
    required this.a,
    required this.r,
    required this.g,
    required this.b,
  });

  int a;
  int r;
  int g;
  int b;
  // ...
}

Even though it would not have been an error to include the original .fromARGB constructor, Pigeon opted not to do so. This makes porting existing code to Pigeon difficult, and if the class is part of the public API, it's impossible to feed it through Pigeon without forcing a breaking change.

Proposal

When generating constructors, Pigeon should first check if the unnamed constructor already exists, and if so, copy it as-is instead of overwriting it. When other constructors exist, Pigeon should copy those, even if it decides to generate its own unnamed constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions