-
Notifications
You must be signed in to change notification settings - Fork 2
Class tannus.ds.Maybe
Ryan Davis edited this page Aug 1, 2015
·
1 revision
The tannus.ds.Maybe type is an abstract type designed to make working with nullable
values more tolerable. It allows the user to test for null
using field-access, rather than a
literal null-check, along with several useful methods for dealing with null values.
If the underlying value is
null
, then this will befalse
, whereas with a non-null underlying value, you'll gettrue
.
A reference to the underlying value
If the underlying value is
null
, returnsvalue
, otherwise returns the underlying value
If the underlying value is non-null, invokes
func
with the underlying value as it's argument
Returns whether the underlying value is
null
package ;
import tannus.ds.Maybe;
class Main {
/**
* If run with the command-line arguments 'Ryan', 'Davis'
*/
public static function main():Void {
var args = Sys.args();
var first:Maybe<String> = null;
trace(first.exists); // false
trace(first.or('John')); // 'John'
//- Or, some syntactic sugar
trace(first || 'John'); // 'John'
first = args[0];
trace(first.exists); // true
trace(first.value); // 'Ryan'
first.runIf(function(name) {
trace('Hello, $name'); // 'Hello, Ryan'
});
first = null;
/* Won't Run */
if (first)
trace('We has a name');
first = args[0];
/* Will Run */
if (first)
trace('We has a name');
}
}