Prev | Current Page 262 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"


class Person
{
public var name : String;
public function new(n : String)
{
name = n;
}
}
class Item
{
public var name : String;
public var price : Float;
public function new(n : String, p : Float)
{
name = n;
price = p;
}
}
(continued)
130
Part I: The Core Language
typedef HasName = {
name : String
}
class Main
{
public static function handle(o : HasName)
{
trace(o.name);
}
public static function main()
{
var person = new Person(???John???);
var item = new Item(???Laptop PC???, 999.9);
var user = { name : ???Jane??? };
handle(person);
handle(item);
handle(user);
}
}
You can even simplify the declaration removing the typedef definition and replacing the handle
declaration with the following line:
public static function handle(o : { name : String})
typedef s are not embedded in the generated output but only used for type checking at compile time.
They enforce a kind of programming based on conventions where developers are required to build
structures with certain characteristics but are not bounded to formal definitions like interfaces or base
classes.


Pages:
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274