Skip to content

Instantly share code, notes, and snippets.

@KinoAR
Created August 31, 2021 22:05
Show Gist options
  • Save KinoAR/322a296aaf397cf551c3f2b38453e8b6 to your computer and use it in GitHub Desktop.
Save KinoAR/322a296aaf397cf551c3f2b38453e8b6 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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