Extending interfaces - not possible, right? :)

Hi all.

It’s Friday, so I thought I’d try to do something crazy.

We have a split interface/implementation type of API in use, something
like this:

// In public documentation
package acme;
interface Item {
    List<Item> getChildren();
}

// Somewhere the user doesn't have access to, or at least where

the names might change every build.
package com.acme.script.private;
class DefaultItem implements Item {
// … black box …
}

I thought, wouldn’t it be nice if I could graft additional methods
onto this interface, so that anything implementing that interface
would get them for free when used from that script. So I did this:

include_class  'acme.Item'
class Item
  def descendants
    children.map { |child| [ child ] + child.descendants }
  end
end

item = # get an item
item.descendants

And this gives me an error, which is not all that surprising:

<script>:14: Item is not a class (TypeError)
org.jruby.embed.EvalFailedException: Item is not a class
  at 

org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:127)
at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Not surprisingly, if I use the class itself, then it works… but I
guess I was wondering, is there any way to achieve this sort of thing
in a nice way when all the user has access to is the interface?

TX


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Jul 29, 2010 at 8:25 PM, Trejkaz [email protected] wrote:

   List<Item> getChildren();

onto this interface, so that anything implementing that interface
item.descendants
guess I was wondering, is there any way to achieve this sort of thing
in a nice way when all the user has access to is the interface?

Try “module Item … end” instead. We define proxies for Java
interface types as modules on the Ruby side, since interfaces can’t be
instantiated.

/Nick


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs