Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Dynamic example in DynamicAccess #539

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions assets/DynamicAccess.hx
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
class Main {
static public function main() {
var user:haxe.DynamicAccess<Dynamic> = {};

// Sets values for specified keys.
user.set("name", "Mark");
user.set("age", 25);

// Returns values by specified keys.
trace(user.get("name")); // "Mark"
trace(user.get("age")); // 25

// Tells if the structure contains a specified key
trace(user.exists("name")); // true
}
class Test {
static function main() {
var users:haxe.DynamicAccess<User> = {
John: { surname: "Smith", age: 24 },
Patti: { surname: "Mayonaise", age: 16 }
};

// Get user John, throw him a brithday
var john = users.get("John");
john.age++;

trace('John ${john.surname} turned ${john.age}'); // John Smith turned 25

// Add new user
users.set("Mark", { surname: "McCartney", age:30 });

// Whether the structure contains a specified key
trace(users.exists("Mark")); // true

// Output all users
for (name => data in users) {
trace('$name ${data.surname} is ${data.age} years old');
}
}
}

typedef User = { surname:String, age:Int };