The Science of Programming/SwayPresentations/Objects/Examples4

Examples

How about try blocks? Using the Sway proximal block style:

   var error;
   
   try (error)
       {
       a / b;
       }
   else if (error . type == :undefinedVariable)
       {
       println("try block has an undefined variable");
       }
   else
       {
       println("try block has a divide error");
       }

Here's the implementation:

   function try($store,$code,$)
       {
       var result = catch(force($code));
       if (error?(result))
           {
           $store = result;
           if ($ != :null,force(car($)));
           }
       else
           {
           result;
           }
       }

Next Previous Top