title: Combining S4 and S3 methods in a single function
body: <p>What is a good way of defining a general purpose function which should have implementations for both S3 and S4 classes? I have been using something like this:</p>
  
  <pre><code>setGeneric("myfun", function(x, ...){  
      standardGeneric("myfun");
  });
  
  setMethod("myfun", "ANY", function(x, ...) {
      if(!isS4(x)) {
          return(UseMethod("myfun"));
      }
      stop("No implementation found for class: ", class(x));
  });
  </code></pre>
  
  <p>This succeeds:</p>
  
  <pre><code>myfun.bar &lt;- function(x, ...){
      return("Object of class bar successfully dispatched.");
  }
  object &lt;- structure(123, class=c("foo", "bar"));
  myfun(object)
  </code></pre>
  
  <p>Is there a move "native" way to accomplish this? I know we can define S4 methods for S3 classes using <code>setOldClass</code>, however this way we lose the S3 method dispatching in case an object has multiple classes. E.g. (in a clean session):</p>
  
  <pre><code>setGeneric("myfun", function(x, ...){  
      standardGeneric("myfun");
  });
  
  setOldClass("bar")
  setMethod("myfun", "bar", function(x, ...){
      return("Object of class bar successfully dispatched.");
  });
  
  object &lt;- structure(123, class=c("foo", "bar"));
  myfun(object)
  </code></pre>
  
  <p>This fails because the second class of <code>object</code>, in this case <code>bar</code>, is ignored. We could probably fix this by defining formal S4 inheritance between <code>foo</code> and <code>bar</code>, but for my application I would rather want <code>myfun.bar</code> to work out of the box on S3 objects with a class <code>bar</code>.</p>
  
  <p>Either way, things are getting messy, and I guess this is a common problem, so there are probably better ways to do this?</p>