-
Notifications
You must be signed in to change notification settings - Fork 30
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
Minimal implementation to support derived-class copy-with overriding #90
Closed
Adam-Langley
wants to merge
86
commits into
numen31337:master
from
Adam-Langley:feature/support-class-derivation
Closed
Minimal implementation to support derived-class copy-with overriding #90
Adam-Langley
wants to merge
86
commits into
numen31337:master
from
Adam-Langley:feature/support-class-derivation
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add prefix with class name
copyWithNull
* added nullability for generic parameter constraints * added nullability check for typing nullables
* analyzer version is set to 2.0.0 * ci minor corrections * ci corrections * a possible build fix * minor ci correction * changelog * minor
* Switching to flutter_lints + null safety code cleanup * CI corrections * named constructor feature
* basic implementation * minor * prevent unnecessary copyWithNull methods generation * naming corrections * naming + ignore cast_nullable_to_non_nullable * docs + test fixes * docs * docs * release
* fix for copyWith null for a non-nullable value * version bump * minor ci correction * minor ci correction
* Updating analyser version * Release notes
* Lint correction * tests correction
* feat(settings): create settings class with default values * refactor(settings): remove default values in favor of settings from build.yaml * feat(settings): get settings from config and make available globally * refactor(settings): change type of #readClassAnnotation to `CopyWithAnnotation` to assert values * feat: create pubspec overrides to always read from project * feat(target annotation): annotate copy_with annotations with Target for better linting * style: organize code (alphabetize) * feat(positioned): add `isPositioned` property to `FieldInfo` * refactor(positioned): remove positional check and unneeded sort * feat(positioned): add check for positioned to include param name * feat: add tests for positioned fields * refactor(positioned): remove named field exceptions tests * feat(positioned): add set settings for testing * fix(positioned): tests for positioned params * feat(field): create `CopyWithFieldAnnotation` * feat(field): remove default values in favor of `CopyWithFieldAnnotation` default values * refactor(field): remove `immutable` prop and replace with `fieldAnnotation` * refactor(fields) reference new `fieldAnnotation` prop for immutable * feat(nullable): add check for null args and add ignores for file * fix: tests to include new additions * docs: update readme
* removing global Settings object + testing global settings * sort_constructors_first * tests fix * minor corrections * version bump to 5.0.0 to avoid compatibility issues * minor corrections
* Update copy_with_generator.dart * Update pubspec.yaml * remove unnecessary ignore comment. * removed unnecessary cast_nullable_to_non_nullable comment * Update pubspec.yaml * Update copy_with_generator.dart removed unnecessary ! cast * Update pubspec.yaml * Update copy_with_generator.dart ! for non nullable fields * Update pubspec.yaml * Update copy_with_generator.dart * Update pubspec.yaml * Update copy_with_generator.dart * Update pubspec.yaml
* test to cover all mentioned nullability issues * minor * minor * ci correction * use analyse action to output errors correctly to the CI dashboard actions * minor corrections * crashing dynamic test case * minor * show warnings from "dart run test" * Revert "show warnings from "dart run test"" This reverts commit f0d4ae3. * introspecting the class itself about the relevant constructors parameters * have ! only when the field of the class is non-nullable * Roll back feature with nullable field and non-nullable constructor. * tests + introducing an error when fields nullability not in sync with the constructor * remove question mark from dynamic input parameters * version bump
* Fixing use case described in numen31337#79 * version bump * test fix
* migrate to dart 3 * fix a small issue related to dart 3
Current production copy_with_generator does not honour derivation of classes annotated with CopyWith. This means that given class A (copyWith) derived by class B (also copyWith) when an instance of B is cast to A, calling 'copyWith' will return an instance of class A, losing all of the properties of B.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #87 and #65
Current production copy_with_generator does not honor derivation of classes annotated with CopyWith. This means that given class A (copyWith) derived by class B (also copyWith) when an instance of B is cast to A, calling 'copyWith' will return an instance of class A, losing all of the properties of B.
It's clear this can cause issues unless the developer is cognisant.
My specific use case is where copyWith is used to mutate state in Flutter Bloc applications.
When Blocs are responding to events, every event receives the current state as the bloc-declared archetype state (i.e. MyInitialState). The the event handler will cast the state to the class that it is aware of - even though it may currently be an instance of a derived type. When calling "copyWith" all derived values are lost.
Usage
After regenerating your code with this enhancement, add override capabilities to your classes like so:
Effect
Here is a pseudo-code usage example for the above example
Caveats
The PR is indeed a minimal implementation. Some cases not handled are: