Jump to content

The Science of Programming/SwayPresentations/Objects/Examples3

From Wikibooks, open books for an open world

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