Prev | Current Page 258 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

x = 0.1; // does not compile
var pFloat = new Point < Float > (0.1, 0.2);
Anonymous Objects
Anonymous objects are structures of data created without instantiating any particular class. They are
recognized in the haXe type system as pertaining to the anonymous type.
Their declaration is very short because it requires just the identifier ??™ s fields and their values:
var color = { r: 255, g: 255, b: 255 };
The type of color object is :
{ r:Int, g:Int, b:Int }
You can declare the expected type this way:
var color : { r:Int, g:Int, b:Int } = { r: 255, g: 255, b: 255};
(continued)
127
Chapter 5: Delving Into Object-Oriented Programming
You can also reuse compatible typedef s like so:
var color : { r:Int, g:Int, b:Int, a:Int } = { r:255, g:255, b:255, a:100 };
var color2 : { r:Int, g:Int, b:Int } = color;
Note that the second type definition contains a field less than the assigned object. This is perfectly legal
because the type definition just imposes the minimum requirements. To create an empty anonymous
object, (an object with no defined fields) use the method Reflect.


Pages:
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270