[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
10.2 Creating and accessing objects
Objects and classes are created and manipulated via library functions and forms created automatically by Bigloo when a new class is defined.
- bigloo procedure: isa? obj class
-
This function returns
#t
if obj is an instance of class or an instance of a sub-class of class, otherwise, it returns#f
.
- bigloo syntax: instantiate::class (ident value)…
-
This forms allocates object of class class and fills the fields with values found in the list of parameters (note that field are explicitly named and that there is no ordering for field naming). Field values which are not provided in the parameter list must have been declared with a
default
value which is used to initialize the corresponding field.For instance:
(module example (export (class point (x (default 0))) (class point2d::point y))) (instantiate::point (x 0) (y 0)) (instantiate::point (y 0)) (instantiate::point (x 0)) ⇒ Error because y has no default value
- bigloo procedure: class-nil class
-
This function returns the NIL pre-existing class instance. This instance plays the role of
void *
in C ornull
in Java. The value of each field is unspecified but correct with respect to the Bigloo type system. Each call toclass-nil
returns the same object (in the sense ofeq?
).(module example (export (class point x) (class point2d::point y))) (eq? (class-nil point) (class-nil point)) ⇒ #t (eq? (class-nil point) (class-nil point2d)) ⇒ #f
- bigloo syntax: with-access::class obj (binding…) body
-
A reference to any of the variables defined in as a binding is replaced by the appropriate field access form. This is true for both reference and assignment. A binding is either a symbol or a list of two symbols. In the first place, it denotes a field. In the second case, it denotes an aliases field.
For instance:
(with-access::point p (x (y1 y)) (with-access::point p2 (y) (set! x (- x)) (set! y1 (- y1 y))))
- bigloo syntax: -> var field
-
Class instances can be accesses using the
->
special form. The the first argument must be the identifier of a local typed variable, otherwise an error is raised. The form->
can be used to get or set value of an instance field. For instance:(define (example p1::point p2::point) (set! (-> p1 x) (- (-> p1 x))) (set! (-> p1 y) (- (-> p1 y) (-> p2 y))))
This is equivalent to:
(define (example p1::point p2::point) (with-access::point p1 (x (y1 y)) (with-access::point p2 (y) (set! x (- x)) (set! y1 (- y1 y)))))
- bigloo syntax: co-instantiate ((var value) …) body
-
This form is only available from compiled modules. In other words, it is not available from the interpreter. It permits the creation of recursive instances. It is specially useful for creating instances for which class declarations contain cyclic type references (for instance a class
c1
for a which a field is declared of classc2
and a classc2
for which a class is declared of typec1
). The syntax of aco-instantiate
form is similar to alet
form. However the only legal values areinstantiate
forms. The variables introduced in the binding of aco-instantiate
form are bound in body. In addition, they are partially bound in the values expressions. In a value position, a variable var can only be used to set the value of a field of an instantiated class. It cannot be used in any calculus. Example:(module obj-example (export (class c1 a b o2::c2) (class c2 x y o1::c1))) (co-instantiate ((o1 (instantiate::c1 (a 10) (b 20) (o2 o2))) (o2 (instantiate::c2 (x 10) (y 20) (o1 o1)))) (with-access::c1 o1 (o2) (with-access::c2 o2 (x y) (+ x y)))) ⇒ 30
- bigloo syntax: duplicate::class obj (ident value)…
-
This forms allocates an instance of class class. The field values of the new object are picked up from the field values of the old object unless they are explicitly given in the parameter list.
For instance:
(with-access::point old (x) (instantiate::point (x x) (y 10)))
is equivalent to:
(duplicate::point old (y 10))
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.