Skip to content

How to use typedef inside a class with Generics? #4151

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

Closed
tolotrasamuel opened this issue Nov 5, 2024 · 1 comment
Closed

How to use typedef inside a class with Generics? #4151

tolotrasamuel opened this issue Nov 5, 2024 · 1 comment
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists

Comments

@tolotrasamuel
Copy link

How to use typedef with generics ?

How to use typedef inside a class ?

I have the following class.

class Bar<T> {
  void bar() {
    print(T);
  }
}

class Foo<T> {
  Bar<T> merge(Bar<T> a, Bar<T> b, Bar<T> c) {
    a.bar();
    b.bar();
    return c;
  }
}

To avoid repeating Bar<T> I want to use typedef

class Foo<T> {

  typedef Baz = Bar<T>;
  
  Baz merge(Baz a, Baz b, Baz c) {
    a.bar();
    b.bar();
    return c;
  }
}

But this is obviously not working with the current Dart. Is it supported ? I know I create an alias of the Bar to a shorter string but this does not remove the generic, the problem is in reality, the generic has several nested class like Foo<Bar<Baz<Foz<Boz<T>>>>

@tolotrasamuel tolotrasamuel added the request Requests to resolve a particular developer problem label Nov 5, 2024
@mateusfccp
Copy link
Contributor

mateusfccp commented Nov 5, 2024

How to use typedef with generics ?

typedef Foo<T> = Bar<T>;

How to use typedef inside a class ?

That's not possible, by definition.

A type alias is a top-level declaration, which means it can only appear [sic] in the top-level scope.

image

There's already an issue for this case: #67 (see also #2952), so I think this can be closed.

@lrhn lrhn added the state-duplicate This issue or pull request already exists label Nov 6, 2024
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants