Warning: readfile(/var/www/header.html) [function.readfile]: failed to open stream: No such file or directory in /var/www/website/fragments/fragments.inc on line 6
12 Dylan Fragments: Classes and make()
Warning: readfile(/var/www/menu.html) [function.readfile]: failed to open stream: No such file or directory in /var/www/website/fragments/fragments.inc on line 8

[ All Fragments ]

Classes and make()

Dylan makes it easy to deal with objects. You don't normally need to write constructors or accessor functions, which saves a lot of boring typing. You can initialize slots (a.k.a. "member variables") using keyword arguments, default values or full-fledged expressions. A small percentage of classes still need constructors, which can be defined by overriding initialize.

define class <car> (<object>)
  slot serial-number :: <integer> = unique-serial-number();
  slot model-name :: <string>,
    required-init-keyword: model:;
  slot has-sunroof? :: <boolean>,
    init-keyword: sunroof?:,
    init-value: #f;
end class <car>;

define variable *unique-serial-number* = 0;

define function unique-serial-number() => (usn :: <integer>)
  let serial = *unique-serial-number*;
  *unique-serial-number* := *unique-serial-number* + 1;
  serial;
end function;

define constant $blue-car = make(<car>, model: "Viper");
define constant $black-car = make(<car>, model: "Town Car", sunroof?: #t);
define constant $red-car = make(<car>, model: "F40", sunroof?: #f);