Created
August 31, 2021 22:05
-
-
Save KinoAR/322a296aaf397cf551c3f2b38453e8b6 to your computer and use it in GitHub Desktop.
Advanced Example of the power of Struct Init
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can test out the code above right here: https://try.haxe.org/#5Fb7F1f4