getx icon indicating copy to clipboard operation
getx copied to clipboard

Replace class declaratrion with typedef

Open Andrflor opened this issue 3 years ago • 2 comments

From dart 2.13 typedef is now a thing. Using class extensions for declaring RxBool, RxString.... is breaking generics. Also boilerplate code is reduced with no more need to create class specific obs extension.

The main idea is to make RxBool, RxString... what they should be, type aliases for Rx<bool> Rx<String>...

This make pretty convenient functions easy to write without having to care about any specificity what so ever. For example i included a pipe function that allow to pipe one observable into another.

Andrflor avatar Apr 14 '22 09:04 Andrflor

1- I agree with this, and I think this is better than using inheritance (current approach).

2- However, some Rx have different behavior, we can use Extensions for that. For example, the toString() method of an RxBool, will print true/false instead of printing (instance of RxBool).

We can add typedef to types, and add content inside extensions. I'm not sure if this is possible, but it might be an alternative.

jonataslaw avatar Apr 14 '22 19:04 jonataslaw

If you are not overriding stuff it's not a problem. The only override so far is the toString method. (Rx iterables are out of scope).

For this toString case if you want the value to be printed why not just declare?

class Rx<T> extends _RxImpl<T> {
  Rx(T initial) : super(initial);

  @override
  dynamic toJson() {
    try {
      return (value as dynamic)?.toJson();
    } on Exception catch (_) {
      throw '$T has not method [toJson]';
    }
  }

  @override
  String toString() => value.toString();
}

Or if only for booleans why not using type asset?

class Rx<T> extends _RxImpl<T> {
  Rx(T initial) : super(initial);

  @override
  dynamic toJson() {
    try {
      return (value as dynamic)?.toJson();
    } on Exception catch (_) {
      throw '$T has not method [toJson]';
    }
  }

  @override
  String toString() =>
      value is bool? ? value.toString() : super.toString();
}

Andrflor avatar Apr 15 '22 06:04 Andrflor