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

implements vs @Implements #1126

Closed
debiotech4ayo opened this issue Sep 25, 2024 · 3 comments
Closed

implements vs @Implements #1126

debiotech4ayo opened this issue Sep 25, 2024 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation needs triage

Comments

@debiotech4ayo
Copy link

debiotech4ayo commented Sep 25, 2024

import 'package:freezed_annotation/freezed_annotation.dart';

// Use the following command to regenerate the freezed file:
// dart run build_runner build
part '../../generated/domain/entities/implemented.freezed.dart';

abstract class Implemented {
  int get value;

  /// Compute a value
  int compute();
}

@freezed
class ImplementedClass with _$ImplementedClass implements Implemented {
  @Implements<Implemented>()
  const factory ImplementedClass({
    required int value,
  }) = _ImplementedClass;

  const ImplementedClass._();

  @override
  int compute() => value;
}

I have some questions about inheritance / implementation with freezed after reading this part and testing a bit.

Does it make sense to mix implements keyword with @Implements ? implements keyword permit me to retrieve the base class documentation.

Additionally it seems we can use implements without @Implements which could be counter-intuitive as missing @Implements makes the generated code not adding the implements Implemented.

@freezed
class ImplementedClass with _$ImplementedClass implements Implemented {
  const factory ImplementedClass({
    required int value,
  }) = _ImplementedClass;

  @override
  int compute() => value;
}

Gives :

abstract class _ImplementedClass implements ImplementedClass {
  const factory _ImplementedClass({required final int value}) =
      _$ImplementedClassImpl;

  [...]
}

However, no error is raised.

I'm not sure to understand...

@debiotech4ayo debiotech4ayo added documentation Improvements or additions to documentation needs triage labels Sep 25, 2024
@rrousselGit
Copy link
Owner

@Implements is for unions, to have constructor implement an interface but not the others.

If you want the whole class to implement an interface, just use implements

@debiotech4ayo
Copy link
Author

However, it's not possible to make a freezed class extends another class, freezed or not, right ?

@rrousselGit
Copy link
Owner

Correct, because there's no good syntax for the super constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation needs triage
Projects
None yet
Development

No branches or pull requests

2 participants