Skip to content

Instantly share code, notes, and snippets.

@zaynyatyi
Created August 26, 2016 13:16
Show Gist options
  • Save zaynyatyi/ce5b7a233de60374be75a05b4134f7e5 to your computer and use it in GitHub Desktop.
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
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