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

Separate between Data Class and Sealed Class #587

Closed
realshovanshah opened this issue Jan 21, 2022 · 5 comments
Closed

Separate between Data Class and Sealed Class #587

realshovanshah opened this issue Jan 21, 2022 · 5 comments
Labels
enhancement New feature or request needs triage

Comments

@realshovanshah
Copy link

Reopening #576

As the title say, a condition to generate only Data Class or Sealed Class would be really helpful for those looking for only one of the use case.

Now, to get to the actual issue that I was facing:

Screen Shot 2022-01-21 at 16 37 49

This was the precise message that the compiler was showing: Try adjusting the supertypes of this class to remove the inconsistency.dart(inconsistent_inheritance_getter_and_method)

The class the I am extending from, let's call it class Parent, already has a copyWith method. (As classes often do.) The external Parent class is not in my control, so I cannot remove or shadow the copyWith method from it. But the freezed class is something that I have control over so I argue for separation between data & sealed class. Or maybe make it optional in the same way maybeWhen and maybeMap are.

I did try some other workaround but didn't find any way to shadow the copyWith method.
To be precise, I tried make it protected with the @Protected annotation but was of no help. And I don't think there's any way of choosing which method to use when using inheritance.

I am currently at a roadblock so if anyone has previously solved parent classes that expose a method with a same name.

@realshovanshah realshovanshah added enhancement New feature or request needs triage labels Jan 21, 2022
@rrousselGit
Copy link
Owner

Inheriting Freezed classes is not supported.
For the simple reason that when using factory constructors, we cannot specify "super"

@realshovanshah
Copy link
Author

realshovanshah commented Jan 21, 2022

Do you mean that I am not able to extend my freezed data classes with any other class?
Because, when I do so, the compiler doesn't complain and the build runner works just fine too.
P.S.: I won't need to use the "super" keyword for the above code, I just want to tell the compiler that the class belong to a known/supported type.

@rrousselGit
Copy link
Owner

Even without the need for "super", it's still not supported to subclass freezed classed

As you've seen, you'll get a clash on various properties like copyWith and possibly the when/map functions

@realshovanshah
Copy link
Author

It actually compiles fine if there are no name collisions. Am I missing something here?
Screen Shot 2022-01-21 at 22 48 30

In the previous screenshot, it's not compiling because the generated freezed mixin has a copyWith method. So, what do you think about #576 ? This would be really helpful for avoiding name collisions.

@rrousselGit
Copy link
Owner

Closing in favor of #636

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

No branches or pull requests

2 participants