cs theory question - wrapping inheritance

non-programmers may skip.

I'm curious; is there a language that allows you to extend a class by wrapping it?

That is, a language that lets me type:
class ExtraFunctionality wraps BaseSystemObject
{
  ExtraFunctionality(BaseSystemObject wrapped_object)
  {
    wrap(wrapped_object)
  }

  //override
  void do_an_awesome_thing()
  {
    super.do_an_awesome_thing();
    do_extra_things_too();
  }
  
  void do_extra_things_to()
  {
    //...
  }

  void do_a_lot_of_things()
  {
    super.do_easy_thing(1);
    super.do_easy_thing(2);
    super.do_easy_thing(3);
  }
}
So that I can then write 
ExtraFunctionality my_obj = new ExtraFunctionality(api.get_base_system_object());
my_obj. do_an_awesome_thing();
my_obj.do_a_lot_of_things();
my_obj.another_base_class_method();
api.use_base_system_object(my_obj);
I can't simply extend the class because I'm not constructing it myself, I'm getting it from the system, so it won't cast.  If I wrap it using a conventional class, then I either have to override everything myself, or else expose the wrapped object and let the caller decide how to deal with it, which is ugly.  Either way though, I can't pass it back into the system, because it's not the right type.

So, I want to inherit the type in the form of a lightweight, easy to write wrapper.  I think this case is getting more relevant these days as APIs and SDKs are coming to dominate the coding landscape, as opposed to working mostly within code you control yourself.  Comments?

1 comment:

  1. Firstly, I think it's a bad idea to break strong typing; i.e. allows a client method to change the behavior of a library typed class. The better thing to do would be to attach callback to the predetermined override-able function whose return value determines invocation of base class function or not.

    But to answer the specific question, C# "extension methods" aren't quite what you're describing.

    It's basically dynamic dispatch, found in highly abstract (slow) languages.

    Dynamic Dispatch

    ReplyDelete