Agora-S Tutorial2

Prof. Dr. Theo D'Hondt
Last revision: August 7th, 1996

Agora-S Tutorial2


Slots of blocks can also be method declarations (i.e. unary, operator or keyword). The body of such a method is a block that will be executed when the method is called by a message send. When the block contains code, the method is executed like in any other OOPL. However, blocks can also contain methods. In this case, the receiver is extended with the methods. As a mather of fact, even when a method does not contain any other methods, the receiver is also temporarily extended. This extension can be returned by returning µself. In brief, when a method contains other methods and it returns µself, the method actually acts as a mixin method.

The syntax of Agora-S requires that every method is annotated by its receiver. In other languages (and also in Agora94) this receiver is usually a hidden parameter called 'self'. In Agora-S one can give 'self' any name just be choosing that name as the receiver of a method. This is heavily used in the following example about complex numbers:

[ Self makeComplex
         µmethod:
              [ realP µvar: 0.0;
                imagP µvar: 0.0;
                self real: realA imag: imagA
                       µmethod: 
                          [ ((self µclone) real: realA) imag: imagA ];
                self real: arg
                       µmethod: 
                          [ realP µ<- arg;
                            self ];
                self imag: arg
                       µmethod: 
                          [ imagP µ<- arg;
                            self ];
                self real
                       µmethod: 
                          [ realP ];
                self imag
                       µmethod: 
                          [ imagP ];
                self mod 
                       µmethod: 
                          [ (realP sqr + imagP sqr) sqrt ];
                self + arg 
                       µmethod: 
                          [ self real: (arg real + realP) 
                                 imag: (arg imag + imagP) ];
                self extend µmethod: [ self print µmethod: [(self real) display; (self imag) display]; µself];
                µself];
                           
  Complex µvar: Object makeComplex;
                 
  c1 µvar: Complex real: 1.5 imag: 2.0;
  c2 µvar: Complex real:-2.0 imag: 1.5;
  c3 µvar: c1 + c2;
  "real part = " display;
  (c3 real) display;   
  eoln display;
  "imag part = " display;
  (c3 imag) display;
  c3 µ<- c3 extend;
  c3 print;
  eoln display] µvalue

The following example can be FTP'ed together with Agora-S: