Download The Grace Programming Language Draft Specification
Transcript
Revision 1261 committed on 2013-08-14 00:06:00Z by kim
7.1
11
Objects
Objects are created by object literals. The body of an object literal consists
of a sequence of declarations.
object {
def colour:Colour = Colour.tabby
def name:String = "Unnamed"
var miceEaten := 0
method eatMouse {miceEaten := miceEaten + 1}
}
Object literals are lexically scoped inside their containing method, or
block. In particular, any object-level expressions, initializer expressions and
method bodies are executed in that lexical context. Each time an object
literal is executed, a new object is created.
Object literals are expressions that evaluate to an object with the given
features. Object literals can also contain statements, which are executed as
a side-effect of evaluating the object literal. Thus, constant can be defined
by an object literal, like this:
def unnamedCat = object {
def colour : Colour = Colour.tabby
def name : String = "Unnamed"
var miceEaten := 0
method eatMouse {miceEaten := miceEaten + 1 }
print "The Cat {name} has been created."
}
to bind a name to an object. Repeated invocations of the reader method
unnamedCat return the same object.
Andrew IDo all of the names declared in a scope come into existence simultaneously when that scope is first entered? J
James IThat’s the aim. Minigrace does
not do that.J
7.2
Classes
Class declarations combine the definition of a factory object with the definition of a single object creation method on that object. This method creates
“instances of the class”. A class declaration is syntactically a combination
of an object literal and a method declaration; everything in the body of the
class declaration can be imagined as being part of an implicit object literal
that is executed each time an “instance of the class” is created.