Skip to content

Commit

Permalink
feat: add a () type and single instance
Browse files Browse the repository at this point in the history
Like the Rust () type and value, useful when a Result has no meaningful
value to return, and returning a void is basically impossible with type
parameterization.

dart run test passes
  • Loading branch information
nlfiedler committed Apr 17, 2021
1 parent 3529bf6 commit a692c01
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This file follows the convention described at
[Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [4.2.0] - 2021-03-12
## [Unreleased]
### Added
- lemunozm: added `isSome()`, `isNone()` to `Option`.
- lemunozm: added `isOk()`, `isErr()` to `Result`.
- lemunozm: added `Option.from()` and `Option.toNullable()` to make easy conversions with nullable values.
- Added a `Unit` type that is similar to Rust's `()` type.

## [4.1.0] - 2021-03-09
### Changed
Expand Down
16 changes: 16 additions & 0 deletions example/unit_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:oxidized/oxidized.dart';

// Insert an entry in a thing and return "nothing" when successful.
Result<Unit, Exception> insertEntry(String name) {
// perform some operation here...
return Ok(unit);
}

void main() {
var result = insertEntry('SampleEntry');
if (result is Err) {
print('oh no, unable to insert the entry!');
} else {
print('nothing much to say about unit result');
}
}
12 changes: 12 additions & 0 deletions lib/oxidized.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,19 @@
/// );
/// }
/// ```
///
/// # Unit
///
/// Similar to the Rust `()` type, the `Unit` type has exactly one value `()`,
/// and is used when there is no other meaningful value that could be returned.
/// This is especially useful for `Result` when not returning anything other
/// than an error, as returning `void` in Dart is difficult with type
/// parameterization.
///
/// The single instance of `Unit` is defined as the `unit` constant in the
/// package.
library oxidized;

export 'src/option.dart';
export 'src/result.dart';
export 'src/unit.dart';
26 changes: 26 additions & 0 deletions lib/src/unit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (c) 2020 Nathan Fiedler
//
import 'package:equatable/equatable.dart';

/// The `Unit` type has exactly one value `()`, and is used when there is no
/// other meaningful value that could be returned for a `Result`.
///
/// For instance, returning a "nothing" `Result` would be as simple as
/// `Result.ok(unit)` and its type would be `Result<Unit, ..>` for whatever type
/// of error the result represents.
class Unit extends Equatable {
const Unit._internal();

@override
List<Object> get props => [];

@override
bool get stringify => true;

@override
String toString() => '()';
}

/// The one instance of `Unit`.
const Unit unit = Unit._internal();
7 changes: 7 additions & 0 deletions test/result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ void main() {
expect(result, isNot(isA<Err>()));
});

test('unit value is an ok unit', () {
var result = Result.ok(unit);
expect(result, isA<Ok>());
expect(result, isNot(isA<Err>()));
expect(result.unwrap(), equals(unit));
});

test('err is not okay', () {
expect(Result.err(Exception()), isA<Err>());
expect(Err(Exception()), isA<Err>());
Expand Down

0 comments on commit a692c01

Please sign in to comment.