[Next] [Previous] [Up] [Top] [Contents] [Index]

2 Dynamically-Typed Core

2.2 Methods

The following definitions expand the earlier shape hierarchy with some methods:

object shape;
  method draw(s, d) { (-- draws s on display d --) }
  method move_to(s, new_center) { (-- move s to new_center --) }

object circle isa shape;
  method area(c@circle) { c.radius * c.radius * pi}
  method circum(c@circle) { c.radius * 2 * pi }

object rectangle isa shape;
  method area(r@rectangle) { r.length * r.width }
  method circum(r@rectangle) { 2 * r.length + 2 * r.width }
  method draw(r@rectangle, d@Xwindow) {
    (-- override draw for the case of drawing rectangles on X windows --) }

object rhombus isa shape;

object square isa rectangle, rhombus; 	-- inherits area method, but overrides circum
  method circum(s@square) { 4 * s.length }

The syntax for method declarations (again, excluding aspects relating to static typing and encapsulation) is as follows:

method_decl	::=	"method" method_name "(" [formals] ")" {pragma}
		    "{" (body | prim_body) "}" [";"]
method_name	::=	msg_name | op_name
msg_name	::=	name
formals	::=	formal { "," formal }
formal	::=	[name] specializer	formal names are optional, if never referenced
specializer	::=	"@" named_object	specialized formal
	|	empty	unspecialized formal

(op_name is the token for infix and prefix operators beginning with a punctuation symbol; see appendix A.2 for more details.)

As a convention, we indent method declarations under the associated object declaration. This has no semantic implication, but it helps to visually organize a collection of object and method declarations in the absence of a more powerful graphical programming environment [Chambers 92b].