-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Doc/do notation documentation (#140)
- Loading branch information
Showing
4 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
examples/do_constructor_pitfalls/bin/do_constructor_pitfalls.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:fpdart/fpdart.dart'; | ||
|
||
/// This file demonstrates some common pitfalls when using the `Do` constructor in the `fpdart` package. | ||
/// These are practices that should be avoided when using it. | ||
void main(List<String> args) {} | ||
|
||
/// This function demonstrates that the `Do` constructor should not contain a `throw` statement. | ||
/// Throwing an exception inside a `Do` constructor can lead to unexpected behavior and should be avoided. | ||
/// Instead, use the `Option` type to handle errors. | ||
void doConstructorShouldNotContainThrow() { | ||
const testOption = const Option<String>.of('test'); | ||
Option.Do( | ||
($) { | ||
if ($(testOption) == 'test') { | ||
// Do not throw inside a Do constructor | ||
throw Exception('Error'); | ||
} | ||
return 'success'; | ||
}, | ||
); | ||
} | ||
|
||
/// This function demonstrates that the `Do` constructor should not contain an `await` statement without executing the `$` function. | ||
void doConstructorShouldNotAwaitWithoutExecutingDollarFunction() { | ||
Future<String> future = Future.value('test'); | ||
const testOption = const Option<String>.of('test'); | ||
Option.Do( | ||
($) async { | ||
// Do not use `await` without executing the `$` function | ||
await future; | ||
return $(testOption); | ||
}, | ||
); | ||
} | ||
|
||
// This function demonstrates that the `Do` constructor should not be nested. | ||
/// Nesting `Do` constructors can lead to unexpected behavior and should be avoided. | ||
void doConstructorShouldNotBeNested() { | ||
const testOption = const Option<String>.of('test'); | ||
// Do not nest Do constructors | ||
Option.Do( | ||
($) => Option.Do( | ||
($) => $(testOption), | ||
), | ||
); | ||
} | ||
|
||
/// This function demonstrates that the `Do` constructor should not call the `$` function inside a callback. | ||
void doConstructorShouldNotCallDollarFunctionInCallback() { | ||
const testOption = const Option<List<String>>.of(['test']); | ||
Option.Do( | ||
($) => $(testOption).map( | ||
// Do not call the `$` function inside a callback | ||
(stringValue) => $(optionOf(stringValue)), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: fpdart_do_constructor_pitfalls | ||
publish_to: none | ||
version: 0.1.0 | ||
homepage: https://www.sandromaglione.com/ | ||
repository: https://github.com/SandroMaglione/fpdart | ||
description: Example of Functional programming in Dart and Flutter using fpdart. Pitfalls to avoid when using the do constructor. | ||
|
||
environment: | ||
sdk: ">=3.0.0 <4.0.0" | ||
|
||
dependencies: | ||
fpdart: | ||
path: ../../packages/fpdart | ||
|
||
dev_dependencies: | ||
lint: ^2.1.2 | ||
test: ^1.24.3 | ||
mocktail: ^0.3.0 |
1 change: 1 addition & 0 deletions
1
examples/do_constructor_pitfalls/test/do_constructor_pitfalls_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters