Skip to content

Macros: Allow extendsType to extend a class generated by the macroΒ #59726

@tylandercasper

Description

@tylandercasper

I'm trying to make a macro that generates getters and setters that the user can easily initialize in the constructor.

The only way to do that, as far as I am aware, is to do something like:

class GeneratedParentClass {
  int? _x;
  int? get x => _x;
  set x(int? val) {
    _x = val;
    //Do something interesting
  }

  GeneratedParentClass(int? x) : _x = x {
    this.x = x;
  }
}

class BaseClass extends GeneratedParentClass {
  BaseClass(super.x);
}

in the build_runner system, this works pretty well, but in Macros, this appears to be impossible.

unlike appendMixins or appendInterefaces, extendsType requires a NamedTypeAnnotationCode, which requires an identifier.
I tried to make a FakeIdentifier which extended Identifier, and took a string, but unfortunately that crashed the inspector.

I think the ability to use created classes for extension would make macros significantly more robust and intuitive, since it would allow for a couple things:

  1. overrriding methods created by the macro.
    For example, if a user wants to modify the result of a generated toJson() call, they could simply do:
  @override
  toJson() {
    var json = super.toJson();
    //modify json as desired
  }
  1. Guarantee the ability for the macro to initialize code as needed.

  2. allow generated variables to be set in constructors. (Which is very important for most widget based code)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions