dart-lang / dart-lang/source_gen

Question: how to deal with type hierarchy

Open
#423 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
494
Forks
115
Avg merge
12h 13m
Merged PRs (30d)
3

Description

Imagine this scenario.
I have a class _BaseClass and _SubClass. I want to autogenerate subclasses for both so that I the end I'd have this hierarchy:

```dart
_BaseClass
BaseClass // auto generated
_SubClass
SubClass // autogenerated
```

I'd declare my typed classes as:
```dart
class _BaseClass
```
and
```dart
class _SubClass: BaseClass
```

That almost works except for the fact that I can't get supertype of _SubClass at build time. I assume that happens because build_runner (or source_gen) at the start of generation first deletes autogenerated files (_BaseClass_ and _SubClass_ in my case) and then the declaration
```dart
class _SubClass: BaseClass
```
isn't valid anymore since there is no _BaseClass_.
In fact analyzer returns me an Object as _SubClass' supertype, not BaseClass.

On possible workaround would be to use a custom attribute, i.e.

```dart
@hierarchy(superclass: 'BaseClass')
class _SubClass: BaseClass
```

But this solution is a bit more error prone. Are there any better solutions?

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the hierarchy with build_runner and source_gen, then inspect how the analyzer resolves _SubClass's supertype while generated files are removed. Compare that behavior with the proposed custom hierarchy attribute; done means identifying and documenting a reliable supported approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.