Created
August 26, 2016 13:16
-
-
Save zaynyatyi/ce5b7a233de60374be75a05b4134f7e5 to your computer and use it in GitHub Desktop.
Just a good example to describe why you will like haxe abstracts from @nadako
This file contains hidden or 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
abstract UserId(Int) { | |
inline function new(id) { | |
this = id; | |
} | |
public static inline function generate():UserId { | |
return new UserId(Std.random(100)); | |
} | |
} | |
abstract Money(Int) { | |
public inline function new(name) { | |
this = name; | |
} | |
public inline function toString() { | |
return 'USD$this'; | |
} | |
} | |
class Test { | |
static function main() { | |
var user = UserId.generate(); | |
var money = new Money(15); | |
giveMoney(user, money); | |
// Error: Money should be UserId (не перепутаешь) | |
// giveMoney(money, user); | |
} | |
static function giveMoney(user:UserId, amount:Money) { | |
trace('Giving $amount money to user $user'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment