hasNext())
{
var letter = sequence.next();
// ...
}
(continued)
133
Chapter 5: Delving Into Object-Oriented Programming
This automatically happens every time an object conforming to the Iterator < T > typedef is used in a
for statement. The same treatment is reserved for the objects that conform to the Iterable < T >
typedef . This can be useful when the object can return a collection but with an intermediate level of
indirection.
The Array < T > is Iterable < T > and can be used to demonstrate its use. The following two declarations
are in fact identical; the compiler internally changes the second to behave like the first.
for(i in [1,2,4,8].iterator())
{
trace(i);
}
for(i in [1,2,4,8])
{
trace(i);
}
Extensions
Extensions are a way to extend classes or anonymous object typedef s on the fly. They are rarely used in
conjunction with classes, because the classic inheritance is usually preferred.
The syntax is the same in both cases where type can be a class or a typedef :
variable = { > Type, /* new field definitions goes here */ }
When using extensions with classes, you will always have to cast the instance to the extended type
because classes do not define types; always pay attention when using casts this way because they are
unsafe casts .
Pages:
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278