diff --git a/examples/fpdart_http/lib/main.dart b/examples/fpdart_http/lib/main.dart index ce8caf5..6542ac4 100644 --- a/examples/fpdart_http/lib/main.dart +++ b/examples/fpdart_http/lib/main.dart @@ -8,7 +8,7 @@ void main() async { Uri.https("pokeapi.co", "/api/v2/pokemon/10"), ) .tap( - (response) => Effect.function( + (response) => Effect.functionSucceed( () => print(response.body), ), ) diff --git a/examples/poke_api/lib/main.dart b/examples/poke_api/lib/main.dart index 3c24ebf..55e4f2c 100644 --- a/examples/poke_api/lib/main.dart +++ b/examples/poke_api/lib/main.dart @@ -68,7 +68,7 @@ void main() async { final exit = await program("9722") .map((pokemon) => print(pokemon)) .catchError( - (error) => Effect.function( + (error) => Effect.functionSucceed( () => print("No pokemon: $error"), ), ) diff --git a/packages/fpdart/analysis_options.yaml b/packages/fpdart/analysis_options.yaml index 4701dcd..d9d369f 100644 --- a/packages/fpdart/analysis_options.yaml +++ b/packages/fpdart/analysis_options.yaml @@ -3,6 +3,7 @@ include: package:lints/recommended.yaml linter: rules: annotate_overrides: true + prefer_const_constructors: true analyzer: language: diff --git a/packages/fpdart/lib/src/effect.dart b/packages/fpdart/lib/src/effect.dart index 076fcee..1b692bd 100644 --- a/packages/fpdart/lib/src/effect.dart +++ b/packages/fpdart/lib/src/effect.dart @@ -62,14 +62,8 @@ final class Effect extends IEffect { /// /// In practice a user of the library should never be allowed to pass `null` as [E]. final FutureOr> Function(E? env) _unsafeRun; - final StackTrace? stackTrace; - static bool debugTracing = false; - - const Effect._( - this._unsafeRun, { - this.stackTrace, - }); + const Effect._(this._unsafeRun); @override Effect get asEffect => this; @@ -83,7 +77,7 @@ final class Effect extends IEffect { R runSync(E env) { final result = _unsafeRun(env); if (result is Future) { - throw Die.current(result, stackTrace); + throw Die.current(result); } return switch (result) { @@ -163,7 +157,12 @@ final class Effect extends IEffect { ); /// {@category constructors} - factory Effect.function(FutureOr Function() f) => Effect._( + factory Effect.functionFail(FutureOr> Function() f) => Effect._( + (_) => f().then(Left.new), + ); + + /// {@category constructors} + factory Effect.functionSucceed(FutureOr Function() f) => Effect._( (_) => f().then(Right.new), ); @@ -174,9 +173,9 @@ final class Effect extends IEffect { factory Effect.succeed(R value) => Effect._((_) => Right(value)); /// {@category constructors} - static Effect unit() => Effect._( - (_) => Right(fpdart_unit.unit), - ); + static Effect unit = Effect._( + (_) => const Right(fpdart_unit.unit), + ); /// {@category collecting} static Effect> forEach( @@ -186,13 +185,13 @@ final class Effect extends IEffect { Effect._( (env) { if (iterable.isEmpty) { - return Right([]); + return const Right([]); } return iterable .mapWithIndex(f) .fold>>( - Effect.succeed(Iterable.empty()), + Effect.succeed(const Iterable.empty()), (acc, effect) => acc.zipWith( effect, (list, r) => list.append(r), @@ -355,7 +354,7 @@ final class Effect extends IEffect { Effect get orDie => Effect._( (env) => _unsafeRun(env).then( (exit) => switch (exit) { - Left(value: final cause) => Left(Die.current(cause, stackTrace)), + Left(value: final cause) => Left(Die.current(cause)), Right(value: final value) => Effect.succeed(value)._unsafeRun(env), }, @@ -424,7 +423,7 @@ extension ProvideNever on Effect { R runSyncNoEnv() { final result = _unsafeRun(null); if (result is Future) { - throw Die.current(result, stackTrace); + throw Die.current(result); } return switch (result) { @@ -445,6 +444,11 @@ extension ProvideNever on Effect { /// {@category execution} Future runFutureNoEnv() async { final result = await _unsafeRun(null); + if (result is! Future) { + print( + "You can use runSync instead of runFuture since the Effect is synchronous", + ); + } return switch (result) { Left(value: final cause) => throw cause, Right(value: final value) => value, @@ -452,5 +456,13 @@ extension ProvideNever on Effect { } /// {@category execution} - Future> runFutureExitNoEnv() async => _unsafeRun(null); + Future> runFutureExitNoEnv() async { + final result = _unsafeRun(null); + if (result is! Future) { + print( + "You can use runSync instead of runFuture since the Effect is synchronous", + ); + } + return result; + } } diff --git a/packages/fpdart/pubspec.yaml b/packages/fpdart/pubspec.yaml index 959e5cd..b952df4 100644 --- a/packages/fpdart/pubspec.yaml +++ b/packages/fpdart/pubspec.yaml @@ -18,7 +18,6 @@ dependencies: dev_dependencies: lints: ^2.0.1 test: ^1.23.1 - glados: ^1.1.6 collection: ^1.17.2 screenshots: diff --git a/packages/fpdart/test/src/effect/effect_collecting_test.dart b/packages/fpdart/test/src/effect/effect_collecting_test.dart index dcc2497..ea12633 100644 --- a/packages/fpdart/test/src/effect/effect_collecting_test.dart +++ b/packages/fpdart/test/src/effect/effect_collecting_test.dart @@ -20,7 +20,7 @@ void main() { final main = Effect.all([ Effect.succeed(10), Effect.fail("10"), - Effect.function(() => mutable += 1), + Effect.functionSucceed(() => mutable += 1), Effect.fail("0"), ]); final result = main.flip.runSync(null); diff --git a/packages/fpdart/test/src/effect/effect_constructors_test.dart b/packages/fpdart/test/src/effect/effect_constructors_test.dart index 646c60b..58da061 100644 --- a/packages/fpdart/test/src/effect/effect_constructors_test.dart +++ b/packages/fpdart/test/src/effect/effect_constructors_test.dart @@ -72,7 +72,7 @@ void main() { test('async succeed', () async { final main = Effect.gen(($) async { final value = - await $.async(Effect.function(() => Future.value(10))); + await $.async(Effect.functionSucceed(() => Future.value(10))); return value; }); final result = await main.runFutureNoEnv(); @@ -81,7 +81,7 @@ void main() { test('fail when running async as sync', () async { final main = Effect.gen(($) { - final value = $.sync(Effect.function( + final value = $.sync(Effect.functionSucceed( () async => Future.value(10), )); return value; diff --git a/packages/fpdart/test/src/effect/effect_sequencing_test.dart b/packages/fpdart/test/src/effect/effect_sequencing_test.dart index 3e1db93..0480bd1 100644 --- a/packages/fpdart/test/src/effect/effect_sequencing_test.dart +++ b/packages/fpdart/test/src/effect/effect_sequencing_test.dart @@ -20,7 +20,7 @@ void main() { test('tap', () { var mutable = 0; final main = Effect.succeed(10).tap( - (_) => Effect.function(() { + (_) => Effect.functionSucceed(() { mutable += 1; }), );