In Haxe, anonymous structures are similar to objects in JavaScript. You use them when you need to create an object quickly. They are lightweight objects that still respect the Haxe type system.

Here’s an example:

var myObj = {
name: 'Timothy',
health: 3
}
//JS Object Example
/*
const myObj = {
name: "Timothy",
health: 3
}
*/
view raw anon.hx hosted with ❤ by GitHub

As you can, they are pretty much the same. But, unlike JavaScript, the Haxe version has a clear advantage.

Anonymous Structures & Type Inference

Anonymous structures unlike JS objects have type inference. That means when you code and try to access the properties on the object, you’ll only get options for name, and health. JavaScript wouldn’t give you these suggestions unless you’re using some plugins in your editor. But that’s only one part of it; it also will throw an error if you don’t follow the structure of the anonymous structure in Haxe when you use it. This feature can help you speed up your coding and especially help reduce silly errors before they happen.

With that said, I hope this helps and use them for quick objects in your code. But if you want to be thorough, create a typedef (type definition).

For more information, you can use the link below.

Anonymous Structures

Stay tuned as there will be more posts in the future getting into the nitty gritty questions you might have with Haxe!

%d bloggers like this: