[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Apr 12 13:13:07 1994 
Subject:Re: flavours 
From:Ian Rogers 
Volume-ID:940412.02 

Monika Sester (monika.sester@ifp.uni-stuttgart.de) wrote:
> a last - perhaps silly question:
> i want to define an object 'line' which is composed of two 'point'
> objects.
> when i create an instance of a new line, i want to make sure, that the
> points do exist. i thought of defining a method 'before initialize', which
> checks for the existance of the points, but i didn't find (up to now), how
> i can get hold of the points.
> perhaps there is also a more elegant way, which does this check
> automatically. e.g. the flavour knows, that it can only have points as
> components, thus the instances assigned to those slots have to be
> points.....

Your last question I could answer off the top of my head, so I've included
some code below. Your other questions were also interesting, but I'll have to
answer them later (unless someone else beats me to it)

Ian
<a href=http://www.cogs.susx.ac.uk/users/ianr>Ian Rogers</a>

uses flavours;

flavour point;
    ivars x, y;
endflavour;

flavour line;
    ivars first = false, last = false;
    define lconstant check_point(point);
        lvars point;
        unless point and isinstance(point, point_flavour) then
            mishap(point, 1, 'Expecting a point');
        endunless;
    enddefine;
    
    defmethod before updaterof first(point);
        lvars point;
        check_point(point);
        point, ;;; push the value back on the stack before continuing
    enddefmethod;
    
    defmethod before updaterof last(point);
        lvars point;
        check_point(point);
        point, ;;; push the value back on the stack before continuing
    enddefmethod;
    
    defmethod after initialise;
        unless ^first then
            make_instance([point x 0 y 0]) -> ^first;
        endunless;
        unless ^last then
            make_instance([point x 0 y 0]) -> ^last;
        endunless;
    enddefmethod;
endflavour;

vars line = make_instance([line]);
line =>
** <instance of line>
line<-first =>
** <instance of point>
'har har har' -> line<-first;
;;; MISHAP - Expecting a point
;;; INVOLVING:  'har har har'
;;; FILE     :  /rsuna/home/ianr/msg.p   LINE NUMBER:  79
;;; DOING    :  check_point line<-first runproc bedstartup