Skip to content

snsvrno/result-hx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Result.hx

A simple return object based on Rust's Result 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<T,E> has two type parameters, T is the main return type while E is the error type.

import result.Result;

function getContent(file : String) : Result<String, String> {
	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

using result.ResultTools;

function main() {
	var content = getContent("afile.txt");

	trace(content.isOk());

	Sys.println('file content: ${content.unwrap()}');

}