2 Dynamically-Typed Core
The syntax for method declarations (again, excluding aspects relating to static typing and encapsulation) is as follows:object
shape;method
draw(s, d) { (-- drawss on display d
--) }method
move_to(s, new_center) { (-- moves to new_center
--) }object
circleisa
shape;method
area(c@circle) { c.radius * c.radius * pi}method
circum(c@circle) { c.radius * 2 * pi }object
rectangleisa
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) { (-- overridedraw for the case of drawing rectangles on X windows
--) }object
rhombusisa
shape;object
squareisa
rectangle, rhombus; -- inheritsarea method, but overrides circum method
circum(s@square) { 4 * s.length }
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].
Generated with Harlequin WebMaker