Created
July 26, 2014 04:36
-
-
Save modalsoul/60b1cbe9c1b9ef57014c to your computer and use it in GitHub Desktop.
Option.getOrElse() like Scala in Haxe.
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
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; | |
} | |
} | |
} |
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
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
$ haxelib run ihx
>> Sample.main();
Sample.hx:10: 1
Sample.hx:11: 0