Previous: , Up: Interface Interop   [Contents]


4.3.1.2 Building Interfaces Around Objects

A consequence of the previous section is that users of GNU ease.js can continue to use strongly typed interfaces even if the objects they are interfacing with do not support ease.js’ interfaces. Consider, for example, a system that uses XMLHttpRequest:

  // modeled around XMLHttpRequest
  var HttpRequest = Interface(
  {
      abort: [],
      open: [ 'method', 'url', 'async', 'user', 'password' ],
      send: [],
  } );

  var FooApi = Class(
  {
      __construct: function( httpreq )
      {
          if ( !( Class.isA( HttpRequest, httpreq ) ) )
          {
              throw TypeError( "Expecting HttpRequest" );
          }

          // ...
      }
  } );

  FooApi( new XMLHttpRequest() );  // okay

Figure 4.6: Building an interface around needed functionality of XMLHttpRequest

This feature permits runtime polymorphism with preemptive failure instead of inconsistently requiring duck typing for external objects, but interfaces for objects handled through ease.js.