Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Advanced Example of the power of Struct Init
//Test it out here: https://try.haxe.org/#5Fb7F1f4
class Test {
public static function main() {
// Uncomment this and see what happens
// var firstUser:Person = {
// name: 'Tim'
// };
// trace(firstUser.name);
var user:Test2 = {
name: 'Tim'
};
trace(user.name);
}
}
@:structInit
class Person {
public var age:Int = 30;
public var name:String = '';
public var color:String = '';
public var eyes:Int = 0;
// Try commenting out this function and see what happens
public function new(?color:String, ?eyes:Int) {
// do stuff like set private defaults
}
}
@:structInit class Test2 {
public var name:String;
var age:Int = 30;
// public function new(name:String, age:Int) {
// this.name = name;
// this.age = age;
// }
public function greet()
trace('Hello, I\'m $name, and I\'m $age years old!');
}
@KinoAR

This comment has been minimized.

Copy link
Owner Author

@KinoAR KinoAR commented Aug 31, 2021

You can test out the code above right here: https://try.haxe.org/#5Fb7F1f4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment