Skip to content

Commit

Permalink
feat: Replace ok with o/okay and err with e/error
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Dec 3, 2024
1 parent a173205 commit 629fa2e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 61 deletions.
10 changes: 5 additions & 5 deletions book/src/libs/result/result.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import 'package:rust/result.dart';
void main() {
final result = processOrder("Bob", 2);
switch(result) {
Ok(:var ok) => print("Success: $ok"),
Err(:var err) => print("Error: $err"),
Ok(:var okay) => print("Success: $okay"),
Err(:var error) => print("Error: $error"),
};
}
Result<String, String> processOrder(String user, int orderNumber) {
final result = makeFood(orderNumber);
if(result case Ok(:final ok)) {
if(result case Ok(o:final order)) {
final paymentResult = processPayment(user);
if(paymentResult case Ok(ok:final paymentOk)) {
return Ok("Order of $ok is complete for $user. Payment: $paymentOk");
if(paymentResult case Ok(o:final payment)) {
return Ok("Order of $order is complete for $user. Payment: $payment");
}
return paymentResult;
}
Expand Down
44 changes: 22 additions & 22 deletions book/src/libs/result/tips_and_best_practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ There are four ways to never unwrap incorrectly:
### Pattern Matching
Simple do a type check with `is` or `case` instead of `isErr()`.
```dart
if (x case Err(:final err)){
return err;
if (x case Err(:final error)){
return error;
}
```
and vice versa
```dart
if (x case Ok(:final ok)){
return ok;
if (x case Ok(:final okay)){
return okay;
}
```
The type check does an implicit cast, and we now have access to the immutable error and ok value respectively.
### Switch
Similarly, we can mimic Rust's `match` keyword, with Dart's `switch`
```dart
switch(x){
case Ok(:final ok):
print(ok);
case Err(:final err):
print(err);
case Ok(:final o):
print(o);
case Err(:final e):
print(e);
}
final y = switch(x){
Ok(:final ok) => ok,
Err(:final err) => err,
Ok(:final o) => o,
Err(:final e) => e,
};
```
### Declaratively
Expand Down Expand Up @@ -103,22 +103,22 @@ like this:
final a, b, c;
final boolResult = boolOk();
switch(boolResult){
case Ok(:final ok):
a = ok;
case Ok(:final o):
a = o;
case Err():
return boolResult;
}
final intResult = intOk();
switch(intResult){
case Ok(:final ok):
b = ok;
case Ok(:final o):
b = o;
case Err():
return intResult;
}
final doubleResult = doubleOk();
switch(doubleResult){
case Ok(:final ok):
c = ok;
case Ok(:final o):
c = o;
case Err():
return doubleResult;
}
Expand All @@ -129,8 +129,8 @@ That is a little verbose. Fortunately, extensions to the recuse, instead do:
final a, b, c;
final result = (boolOk, intOk, doubleOk).toResultEager();
switch(result){
case Ok(:final ok):
(a, b, c) = ok;
case Ok(:final o):
(a, b, c) = o;
case Err():
return result;
}
Expand All @@ -155,10 +155,10 @@ Result<int,String> usingTheEarlyReturnKey() => Result(($){ // Early Return Key
Result<int,String> usingRegularPatternMatching(){
int x;
switch(willAlwaysReturnErr()){
case Err(:final err):
return Err(err);
case Ok(:final ok):
x = ok.toInt();
case Err(:final e):
return Err(e);
case Ok(:final o):
x = o.toInt();
}
return Ok(x);
}
Expand Down
8 changes: 4 additions & 4 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ Result<int, String> usingTheEarlyReturnKeyExample() => Result(($) {

Result<int, String> usingRegularPatternMatchingExample() {
switch (willAlwaysReturnErr()) {
case Err(:final err):
return Err(err);
case Ok(:final ok):
return Ok(ok.toInt());
case Err(:final e):
return Err(e);
case Ok(:final o):
return Ok(o.toInt());
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/result/future_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ extension Result$FutureResultExtension<S, F extends Object>
}

@pragma("vm:prefer-inline")
Future<Option<S>> unwrapOrOption() async {
return then((result) => result.unwrapOrOption());
Future<Option<S>> ok() async {
return then((result) => result.ok());
}

@pragma("vm:prefer-inline")
Expand Down
10 changes: 5 additions & 5 deletions lib/src/result/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ sealed class Result<S, F extends Object> {
/// Converts a [Result] into an Option, returning [Some] if the [Result] is [Ok], and [_None] if the [Result] is [Err].
/// Note: This should not be used to determine is [Ok] or is [Err], since when the success type is nullable, a
/// null is ambiguous in meaning.
Option<S> unwrapOrOption();
Option<S> ok();

/// Returns the err value if [Result] is [Err].
/// Throws a [Panic] if the [Result] is [Ok].
Expand Down Expand Up @@ -232,7 +232,7 @@ final class Ok<S, F extends Object> implements Result<S, F> {
final S o;

@pragma("vm:prefer-inline")
S get ok => o;
S get okay => o;

//************************************************************************//

Expand All @@ -258,7 +258,7 @@ final class Ok<S, F extends Object> implements Result<S, F> {

@override
@pragma("vm:prefer-inline")
Some<S> unwrapOrOption() => Some(o);
Some<S> ok() => Some(o);

@override
@pragma("vm:prefer-inline")
Expand Down Expand Up @@ -447,7 +447,7 @@ final class Err<S, F extends Object> implements Result<S, F> {
final F e;

@pragma("vm:prefer-inline")
F get err => e;
F get error => e;

//************************************************************************//

Expand All @@ -473,7 +473,7 @@ final class Err<S, F extends Object> implements Result<S, F> {

@override
@pragma("vm:prefer-inline")
Option<S> unwrapOrOption() => None;
Option<S> ok() => None;

@override
@pragma("vm:prefer-inline")
Expand Down
14 changes: 7 additions & 7 deletions test/option/result_and_option_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import 'package:rust/rust.dart';
import 'package:test/test.dart';

void main() {
test("ResultOnOptionExtension unwrapOrOption", () {
test("ResultOnOptionExtension ok", () {
Result<int, String> okResult = Ok(5);
expect(okResult.unwrapOrOption(), Some(5));
expect(okResult.ok(), Some(5));

Result<int, String> errResult = Err("Error");
expect(errResult.unwrapOrOption(), None);
expect(errResult.ok(), None);
});

test("OkOnOptionExtension unwrapOrOption", () {
test("OkOnOptionExtension ok", () {
Ok<int, String> okResult = Ok(5);
expect(okResult.unwrapOrOption(), Some(5));
expect(okResult.ok(), Some(5));
});

test("ErrOnOptionExtension unwrapOrOption", () {
test("ErrOnOptionExtension ok", () {
Err<int, String> errResult = Err("Error");
expect(errResult.unwrapOrOption(), None);
expect(errResult.ok(), None);
});

test("ResultOptionExtension transpose", () {
Expand Down
23 changes: 10 additions & 13 deletions test/result/record_to_result_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ void main() {
final a, b;
switch ((boolOk(), intOk()).toResult()) {
case Ok(
:final ok,
:final o,
):
(a, b) = ok;
(a, b) = o;
case Err(e: final _):
throw Exception();
}
Expand All @@ -33,9 +33,9 @@ void main() {
bool hasErr = false;
final a, b;
switch ((boolOk(), intErr()).toResult()) {
case Ok(ok: final o):
(a, b) = o;
case Err(err: final _):
case Ok(okay: final record):
(a, b) = record;
case Err(error: final _):
hasErr = true;
}
expect(hasErr, true);
Expand All @@ -44,8 +44,8 @@ void main() {
test("3 records to result Ok", () {
final a, b, c;
switch ((boolOk(), intOk(), doubleOk()).toResult()) {
case Ok(o: final ok):
(a, b, c) = ok;
case Ok(o: final record):
(a, b, c) = record;
case Err():
throw Exception();
}
Expand Down Expand Up @@ -111,8 +111,7 @@ void main() {
test("5 records to result Err", () {
bool hasErr = false;
final a, b, c, d, e;
switch (
(boolOk(), intOk(), doubleOk(), stringErr(), intOk()).toResult()) {
switch ((boolOk(), intOk(), doubleOk(), stringErr(), intOk()).toResult()) {
case Ok(o: final ok):
(a, b, c, d, e) = ok;
case Err():
Expand Down Expand Up @@ -203,8 +202,7 @@ void main() {
// Case 5: 5 records to Result
test("5 records to result Ok", () {
final a, b, c, d, e;
switch ((boolOk(), intOk(), doubleOk(), stringOk(), intOk())
.toResultEager()) {
switch ((boolOk(), intOk(), doubleOk(), stringOk(), intOk()).toResultEager()) {
case Ok(o: final ok):
(a, b, c, d, e) = ok;
case Err():
Expand All @@ -220,8 +218,7 @@ void main() {
test("5 records to result Err", () {
bool hasErr = false;
final a, b, c, d, e;
switch ((boolOk(), intOk(), doubleOk(), stringErr(), intOk())
.toResultEager()) {
switch ((boolOk(), intOk(), doubleOk(), stringErr(), intOk()).toResultEager()) {
case Ok(o: final ok):
(a, b, c, d, e) = ok;
case Err():
Expand Down
21 changes: 18 additions & 3 deletions test/result/result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ void main() {
});
});

group('unwrapOrOption', () {
group('ok', () {
test('Ok', () {
final result = Ok(0);
final value = result.unwrapOrOption();
final value = result.ok();
expect(value, Some(0));
});

test('Err', () {
final result = Err(0);
final value = result.unwrapOrOption();
final value = result.ok();
expect(value, None);
});
});
Expand Down Expand Up @@ -488,4 +488,19 @@ void main() {
expect(testDoNotation().unwrapErr(), "");
});
});

test("Result shadowing", () {
int outer = 2;
Result<int, String> result = Ok(1);
int value;
switch (result) {
case Ok(o: final outer):
value = outer;
case Err(e: final errorValue):
final _ = errorValue;
throw Exception();
}
expect(value, equals(1));
expect(outer, equals(2));
});
}

0 comments on commit 629fa2e

Please sign in to comment.