rrousselGit / freezed

Code generation for immutable classes that has a simple syntax/API without compromising on the features.
https://pub.dev/packages/freezed
1.94k stars 237 forks source link

implements vs @Implements #1126

Closed debiotech4ayo closed 2 months ago

debiotech4ayo commented 2 months ago
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...

rrousselGit commented 2 months ago

@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 commented 2 months ago

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

rrousselGit commented 2 months ago

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