-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a () type and single instance
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
Showing
5 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
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
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,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'); | ||
} | ||
} |
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
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,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(); |
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