Dylan Inertia screen shot

// Let's define a base class for our objects
define class <actor> (<object>)
// Each slot is initialized in each object instance
slot position = make (<vector>, size: 2);
slot velocity = make (<vector>, size: 2);
slot radius = 1.0;
slot mode = #"normal";
end;
// And here we have a few subclasses of <actor>
define class <ship> (<actor>) end;
define class <rock> (<actor>) end;
// A ship collided with a rock, let's do something
define method actor-collided (ship :: <ship>, rock :: <rock>)
format-out ("Ship collided with a rock!\n");
end;
// No matter what state, advance the actor's position
define method actor-tick (actor :: <actor>, mode :: <symbol>)
actor.position := actor.position + actor.velocity;
end;
// If the ship is in "boost" mode, make it accelerate
define method actor-tick (ship :: <ship>, mode == #"boost")
next-method(); // calls any other applicable methods
ship.velocity := ship.velocity * 1.1;
end;
// Now lets make a ship and a rocks container
let ship = make (<ship>);
let rocks = vector (make (<rock>), make (<rock>));
while (1)
// Advance the ship and each rock, and check for collisions
actor-tick (ship, ship.mode);
for (rock in rocks)
actor-tick (rock, rock.mode);
if (distance-to (ship, rock) < ship.radius + rock.radius)
actor-collided (ship, rock);
end;
end;
end;
-- H. Abelson and G. Sussman,
Structure and Interpretation of Computer Programs,
MIT Press 1985.
define method move-to (self :: <window>, x, y)Into this:
end;
define-method <window>.move-to(x, y)With this little macro:
end;
define macro define-methodNice!
{ define-method ?class:name . ?name:name (?params:*)
?:body
end } =>
{ define method ?name (?=self :: ?class, ?params)
?body
end }
end;
define method handle-event( event :: <button-down>, button == $left ) => ()That is an entangled mess of spaghetti.
[...]
end method;
define method handle-event( event :: <button-down>, button == $right ) =>()
[...]
end method;
And here's the equivalent in pseudo-Java:
void handle_event( Event event, int button ) {
if( event.isInstanceOf( ButtonDown ) ) {
if( button == LEFT_BUTTON ) {
[...]
} else if( button == RIGHT_BUTTON ) {
[...]
}
}
}