6thsolution / dart_sealed

Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
https://pub.dev/packages/sealed_annotations
MIT License
17 stars 9 forks source link

Functional types are not supported as field types #3

Closed easazade closed 3 years ago

easazade commented 3 years ago

here is the code for the sealed class

import 'package:flutter/material.dart';
import 'package:rekab_delivery/src/util/consumable.dart';
import 'package:rekab_delivery/src/util/typedefs.dart';
import 'package:rekab_delivery/src/widget/src/utils/message.dart';
import 'package:sealed_annotations/sealed_annotations.dart';

part 'login_state.sealed.dart';

@Sealed()
abstract class _LoginState {
  void initial();
  void loading();
  void success(List<String> countries);
  void error(Message msg);
  void invalidNumber();
  void retry(VoidCallback callback);
}

and here is the error i get when running build_runner

[INFO] Generating build script...
[INFO] Generating build script completed, took 675ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 112ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 670ms

[INFO] Running build...
[INFO] 1.2s elapsed, 1/3 actions completed.
[INFO] 2.2s elapsed, 1/3 actions completed.
[INFO] 3.3s elapsed, 1/3 actions completed.
[INFO] 10.7s elapsed, 1/3 actions completed.
[INFO] 13.3s elapsed, 2/3 actions completed.
[INFO] 14.5s elapsed, 8/9 actions completed.
[SEVERE] sealed_generators:sealed_generators on lib/src/cubits/login/request/code/login_state.dart:

"SealedError{message={internal error},cause={check failed}}"
[INFO] Running build completed, took 14.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 58ms

[SEVERE] Failed after 14.7s
pub finished with exit code 1

I tried different things found out whenever I use the type VoidCallback or a generic type that has that type like Consumable<VoidCallback> as the method argument, code generation fails

FatulM commented 3 years ago

this will fail:

import 'package:sealed_annotations/sealed_annotations.dart';

part 'state.sealed.dart';

@Sealed()
abstract class _State {
  void one(void Function() x);
}
"SealedError{message={internal error},cause={check failed}}"

I think it's because of the internal checks I put on argument types which I didn't think of function types as valid types! 😢

this will fail for the same reson:

import 'package:sealed_annotations/sealed_annotations.dart';

part 'state.sealed.dart';

typedef void ABC();

@Sealed()
abstract class _State {
  void one(ABC x);
}

for now you can use this workaround which won't fail:

import 'package:sealed_annotations/sealed_annotations.dart';

part 'state.sealed.dart';

typedef void ABC();

@Sealed()
abstract class _State {
  void one(@WithType('ABC') x);
}

I think analyzer unwraps typedefs by default, but by using this workaround we are forcing dart_sealed to use typedef instead.

I should find a way to check functional types, then I can fix this issue. @easazade

easazade commented 3 years ago

@FatulM the solution worked fine

FatulM commented 3 years ago

I've fixed this issue in 1.7.0, Now you should be able to use all functional types, check for example: functional.dart

@easazade @SaeedMasoumi