Last modified on 27 July 2009, at 20:16

The Science of Programming/SwayPresentations/Objects/Examples3

Examples

Being able to extend a class such that the subclass has the same name is a great way to implement aspects.

   extends(stack());                //if subclass has new name
   extends((shadowed(:stack))());   //if subclass has same name

As an alternative, here's an instrumented stack using filters:

   include("stack.s");
   
   function emptyPopChecker($expr)
       {
       if ($expr . context . empty?())
           {
           throw(:stackError,"pop on empty stack");
           }
       force($expr);
       }

Then we make and instrument a particular stack:

   var s = stack();
   
   s . pop . filter = emptyPopChecker;

Not quite right. Anybody?

   function emptyPopChecker2($expr)
       {
       var result = catch(force($expr));
       if (type(result) == :ERROR)
           {
           throw(:stackError,"pop on empty stack");
           }
       result;
       }

Next Previous Top