Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/snsvrno/result-hx
Simple safe return type library
https://github.com/snsvrno/result-hx
haxe library
Last synced: 25 days ago
JSON representation
Simple safe return type library
- Host: GitHub
- URL: https://github.com/snsvrno/result-hx
- Owner: snsvrno
- License: mit
- Created: 2022-04-04T15:02:17.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T22:28:53.000Z (over 1 year ago)
- Last Synced: 2024-11-14T10:57:00.223Z (2 months ago)
- Topics: haxe, library
- Language: Haxe
- Homepage: https://snsvrno.github.io/result-hx/
- Size: 276 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Result.hx
A simple return object based on Rust's [Result](https://doc.rust-lang.org/std/result/enum.Result.html) for easier error handling and making safe code easier.## Usage
**Result-hx** was created to allow for "safer" returning of functions, and better management of errors. `Result` has two type parameters, `T` is the main return type while `E` is the error type.```haxe
import result.Result;function getContent(file : String) : Result {
if (!sys.FileSystem.exists(file)) return Error('file $file does not exist');
else return Ok(sys.io.File.getContent(file));
}function main() {
switch(getContent("afile.txt")) {
case Error(error): Sys.println('error getting content: $error');
case Ok(content): Sys.println('file content: $content');
}
}
```Some helper functions are included in `result.ResultTools`
```haxe
using result.ResultTools;function main() {
var content = getContent("afile.txt");trace(content.isOk());
Sys.println('file content: ${content.unwrap()}');
}
```