Prev | Current Page 251 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

To resolve those extreme situations you may rely on the reflection API
that is described in detail in Chapter 16 .
In the blog entry example, it is not too difficult to spot that the implementation of the createdOn
property is a little bit pointless in the context of a CMS application that uses a database to store and
retrieve the site contents. It is true that creating records in a database table by reading the properties and
the variables one by one is quite easy. But what happens when the object must be loaded from the
database? How is it dealt the assignment of the createdOn property because it is read - only?
Reflect and Type classes can offer a solution to the problem, as shown in the following example:
var entry : Article = Type.createEmptyInstance(Type.resolveClass(???Article???));
Reflect.setField(entry, ???createdOn???, Date.fromString(???2007-04-10???));
The class is instantiated using the Type.createEmptyInstance() , which builds an object without even
invoking the constructor (in this case there would not be a great difference in instantiating the object
123
Chapter 5: Delving Into Object-Oriented Programming
using the new keyword, but there are cases where this can make really a great difference).


Pages:
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263