Anonymous objects are objects that are of an unnamed type ("Anonymous Type"). They are made of a list of fields and their associated values. For example, here is how we can create one:
var obj = {id : 1, name : "Benjamin"};
Although the type of this object doesn't have a name, obj is still fully typed as {id :Int, name : String}
. This is an anonymous type.
This is sometimes useful, if there is a type that you are not going to use a lot of time. By the way, this variable could be declared this way:
var obj : {id : Int, name : String};
Duck typing makes reference to an old cliché of "if it walks and talks like a duck, then it's a duck". In fact, we can use anonymous types to say that we want something that has at least some fields.
The following is an example:
class TestAnonymous { public var name : String; public function new(name : String) { this.name = name; } public static function main(): Void { var m; sayHello(new TestAnonymous...