Skip to content
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

Extend non-freezed base classes #464

Open
derolf opened this issue Jun 15, 2021 · 13 comments
Open

Extend non-freezed base classes #464

derolf opened this issue Jun 15, 2021 · 13 comments
Assignees
Labels
enhancement New feature or request

Comments

@derolf
Copy link

derolf commented Jun 15, 2021

class Foo {
  Foo({required this.foo});

  final int foo;
}

@freezed
class Bar extends Foo with _$Bar {
  const factory Bar({
    required int foo,
  }) = _Bar; // --- how to wire up Foo(foo: foo)?
}

Here, we have a non-freezed base Foo (potentially coming from a 3rd party lib) and want to extend it with a subclass that uses @freezed.

@derolf derolf added enhancement New feature or request needs triage labels Jun 15, 2021
@rrousselGit
Copy link
Owner

Extending any object is not supported at the moment.

@derolf
Copy link
Author

derolf commented Jun 15, 2021

I found a workaround turning Foo into a mixin. But that doesn't work for 3rd party code...

@rrousselGit
Copy link
Owner

Don't use inheritance. Use composition

@freezed
class Bar  with _$Bar {
  const factory Bar({
    required Foo foo,
  }) = _Bar;
}

@rrousselGit
Copy link
Owner

I don't really plan working on this, but if someone wants to, I think we could consider a @Super(..) annotation

We'd probably want to support both:

@Super('foo: foo');
factory ...

@Super('super.named(foo: foo)');
factory ...

@ozum
Copy link

ozum commented Dec 30, 2021

Don't use inheritance. Use composition

Could you expand the example a little bit, please?

@heshaShawky
Copy link

I don't really plan working on this, but if someone wants to, I think we could consider a @Super(..) annotation

We'd probably want to support both:

@Super('foo: foo');
factory ...

@Super('super.named(foo: foo)');
factory ...

Greate news, thanks.

@tmaihoff
Copy link

any update on this?

@abdulrojakdev
Copy link

any update on this?

did you get the solution sir?

@rrousselGit
Copy link
Owner

Like I said before, I don't plan on working on this.

I'm waiting for metaprogramming.

@oravecz
Copy link

oravecz commented Nov 11, 2022

In the meantime, is there a recipe for annotating a composed object when the JSON we are parsing is flat as a serialized inherited would be? Or is that more of a question for the json_serializable folks?

@shtse8
Copy link

shtse8 commented Mar 24, 2023

without inheritance it's hard to create base state in abstract class. composition is not working well in abstract as we don't know what subclasses are doing and there is no way letting them to override super class state type.

@ThangVuNguyenViet
Copy link

ThangVuNguyenViet commented Apr 12, 2023

How do we reuse the json parsing with composition? Switching from inheritance to composition breaks the object hierarchy.

The toJson of inheritance version would be like:

{"fooKey": "foo", "barKey": "bar"}

The toJson of composition version would be like:

{"barKey": "bar", "foo": {"fooKey": "foo"}}

Any way for composition to maintain the same json structure as the inheritance one?

@rrousselGit rrousselGit self-assigned this May 10, 2023
@CobeyH
Copy link

CobeyH commented Jan 26, 2024

How do we reuse the json parsing with composition? Switching from inheritance to composition breaks the object hierarchy.

The toJson of inheritance version would be like:

{"fooKey": "foo", "barKey": "bar"}

The toJson of composition version would be like:

{"barKey": "bar", "foo": {"fooKey": "foo"}}

Any way for composition to maintain the same json structure as the inheritance one?

Could we get an answer to this? I have 5 properties that are shared across all of my classes. The backend provides the json in a flat format so I can't use the nested composition as suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants