Skip to content

Expose default values for parameters to macros? #3305

Closed as not planned
Closed as not planned
@jakemac53

Description

@jakemac53

The macro apis today do not give access to default values for parameters (or initializer expressions for variables either).

When augmenting a function today you are not allowed to specify default values, so we probably don't need them in that case. However, when overriding a method with a new declaration in a class (so you aren't augmenting an existing method/constructor, you are specifying a new one, which is an override), you do likely need access to the default values of the original parameters.

We could likely only provide these as Code objects and not values (the values might be user types, have unresolved identifiers, etc). But that should be sufficient as the use case is really just plopping in the same default value.

Concerns

I am concerned that in the case of unresolved identifiers (that will be produced in phase 2), we can't even always produce a valid augmentation library (at least in phase 1, although it would be challenging even in phase 2).

Consider a macro that runs in phase one, but it creates a type that extends some other type and overrides some members. Generally that would be hard to actually do since you can't introspect over members, but the macro could actually live on a member, something like this:

class A {
  @SomeMacro()
  void foo([int x = y]); // y is generated in phase 2!
}

// Generated in phase one, how do we refer to y properly if we don't know where it comes from?
class B extends A {
  void foo([int x = y])
}

Possibly we could just assume that any unresolved identifier will eventually exist in the current library, and not do any prefix. But that seems sketchy.

Other options

Alternatively, could we add a way of referring to the overridden default value? This would be generally useful, and would avoid the issue entirely. Maybe something like void foo({super.x}) or void foo({int x = super.default})?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions