Skip to content

Instantly share code, notes, and snippets.

@modalsoul
Created July 26, 2014 04:36
Show Gist options
  • Save modalsoul/60b1cbe9c1b9ef57014c to your computer and use it in GitHub Desktop.
Save modalsoul/60b1cbe9c1b9ef57014c to your computer and use it in GitHub Desktop.
Option.getOrElse() like Scala in Haxe.
import haxe.ds.Option;
class ExtendedOption {
public static function getOrElse<T>(opt:Option<T>, defValue:T) {
return switch(opt) {
case Some(v): v;
case None: defValue;
}
}
}
import haxe.ds.Option;
using ExtendedOption;
class Sample {
static var s = Some(1);
static var n = None;
public static function main(){
trace(s.getOrElse(0));
trace(n.getOrElse(0));
}
}
@modalsoul
Copy link
Author

usage:
$ haxelib run ihx
>> Sample.main();
Sample.hx:10: 1
Sample.hx:11: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment