[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
10.1 Class declaration
Classes are defined in a module declaration. A class declaration can take place in a compiled or interpreted module. If a class declaration takes place in a static module clause (see Section Module declaration) its scope is limited to the current module but if it takes place in an export module clause, its scope is extended to all modules that import the current module. The syntax of a class declaration is:
- bigloo module clause: class ident field …
-
<class> →
(class
<ident> <constructor>? <field>+)
|(final-class
<ident> <constructor>? <field>+)
|(wide-class
<ident> <constructor>? <field>+)
|(abstract-class
<ident> <constructor>? <field>+)
<constructor> →(
<expr>)
<field> → <ident> |(
<ident> <field-prop>)
<field-prop> →read-only
|(get
<bigloo-exp>)
|(set
<bigloo-exp>)
|(default
<bigloo-exp>)
|(info
<bigloo-exp>)
A class is a Bigloo type (see Section Atomic types) and the class identifier is extracted from the <ident> of the class definition. If <ident> is also a <typed-ident>, the type part of this identifier denote the super-class of the class. If <ident> is a <IEEE-ident>, the super-class of the class is the root of the inheritance tree, the
object
class. Thisobject
class is the only pre-existing class.Final classes can only be sub-classed by wide classes. Wide classes (only for compiled modules) can only inherit from final classes. abstract classes can’t be instantiated.
Wide-classes cannot be defined within the interpreter.
The optional constructor is an expression that must evaluate to a one argument function. This function is automatically invoked each time a new class instance is created. The constructor is passed the fresh instance. If a class has not defined a constructor the super class’ constructors are searched. The first constructor found is invoked. A constructor may be a generic function with a method specified for one or more classes.
A class field may be a typed class field which is achieved by using a <typed-ident> instead of a <IEEE-ident> for the <ident> value.
Field marked with
read-only
declaration are immutables.Default
declarations allow default field values.For the means of an example, the traditional points and colored points can be defined as:
(module example (static (abstract-class pt) (class point::pt x::double y::double) (class point-C::point (color::string read-only))))
We illustrate final and wide classes by the example:
(module example (export (final-class person (name::string (default "Jones")) (sex read-only) children::pair-nil) (wide-class married-person::person mate::person)))
Fields may be virtual. A field is virtual as soon as its declaration contain a
get
attribute. Virtual fields have no physical implementation within the instance. When defining a virtual field, the class declaration implements a getter and a setter if that field is not a read only field. Access to the virtual field will rely on invocation of the user getter and user setter. For instance:(module example (static (class complex mag::double angle::double (real::double (get (lambda (p) (with-access::complex p (mag angle) (* mag (cos angle))))) read-only) (imag::double (get (lambda (p) (with-access::complex p (mag angle) (* mag (sin angle))))) read-only)))) (let ((p (instantiate::complex (mag 1.0) (angle 2.18)))) (with-access::complex p (real imag) (print "real: " real) (print "imag: " imag)))
Virtual fields cannot be associated default values. If a virtual field is not provided with a setter it must be annotated as read only.
Info
declarations allow arbitrary user information field values. This value can be retrieved by introspection, by the means of theclass-field-info
introspection function.For the means of an example, with add to information to the slot of the point class.
(module example (static (class point (x::double (info '(range 0.0 10.0))) (y::double (info '(range -1.0 1.0)))))
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.