February 2010 Archives

Java: Making Ambiguous References Unambiguous

| No Comments
// *** Key.java ***
interface Key {}

// *** Card.java ***
interface Card {}

// *** KeyCard.java ***
public class KeyCard implements Key, Card {}

// *** Door.java ***
public class Door
{
    public void openWith(Key key) { System.out.println("Opened door with key"); }
    public void openWith(Card card) { System.out.println("Opened door with card"); }
}

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
door.openWith(keycard);
    }
}

What's wrong with this Java code? It won't compile:

$ javac Scenario.java
Scenario.java:15: reference to openWith is ambiguous, both method openWith(Key) in Door and method openWith(Card) in Door match
door.openWith(keycard);
^
1 error

There are two functions called openWith, which is fine, because Java supports overloaded functions. And we are passing an object to a function on the basis of which interface it implements, which is fine, because Java supports polymorphism. We have also implemented more than one interface in the door class, which normally is fine, because Java allows you to implement more than one interface per class, as a replacement for multiple inheritance.

The problem, though, is that in this case we have an overloaded function, openWith, capable of taking an object that implements either the key interface or the card interface, but we passed it an object that implements both the key interface and the card interface, and the compiler does not know what you want it to be passed in as. So it is an ambiguous function call.

I ran into this error in some code at work today. (Real code, not the silliness above.) I wasn't quite sure what to do. One option was to change the names of the functions in the Door class, to something like openWithKey(Key key) and openWithCard(Card card). But that would be effectively abandoning function overloading, which is a really convenient and tidy feature in any language.

Another option would be change the KeyCard class to support only one interface or the other. But I want KeyCard to have more than one interface implemented... that is what makes it such a cool object.

It took me a few minutes of staring at my screen to recognize the simplest, easiest, and most obvious choice. Every object variable in Java is really just a reference to the object right? So why don't we just add a few lines to the Scenario class, like so?:

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
Key key = keycard;
Card card = keycard;
door.openWith(key);
door.openWith(card);
    }
}
Now this compiles and runs:

$ javac Scenario.java
$ java Scenario
Opened door with key
Opened door with card

The variables key, card, and keycard all refer to the same object. That's fine with the compiler... it just needs to know which interface the object is supposed to look like when you pass it in.

So, in summary, make an ambiguous function call unambiguous by aliasing it before-hand to be the specific kind of interface you need at that moment.

First Gentoo Router

| No Comments
I stayed up late last night setting up my first Gentoo-based home router out of an old PC (500 Mhz Pentium III, 256 MB RAM). It has three interfaces (two LAN, one WAN), is fully optimized for the processor, and provides DHCP, DNS, and NAT services.

It was overall fairly easy following the online Gentoo documentation:

http://www.gentoo.org/doc/en/home-router-howto.xml

The longest part was all the time I spent trimming down the kernel (not strictly necessary but a lot of fun) and then it takes a long time for the kernel to build on a 500 Mhz box. It took probably 3-4 of my attention, including putting the PC together and testing the services, and about 7+ hours of computer time.

Uh, no... Very Different.

| No Comments
So I'm sitting on some stair steps at school, and I turn my laptop on, which is running Gentoo. The boot phase starts as usual, and the text scrolls by as the services start up.

A large, young fellow walks by, sees the screen, and says "Is that MS-DOS?"

"No," I quickly reply, "It's Linux."

"Oh. They're basically the same, aren't they?" he says.

Ouch, that hurt.

Nifty! Custom-Built Linux Computers

| No Comments
system76

What attracted my attention was the server section, where you can by tower or rack mounted servers starting at around $700. Everything is Linux compatible, and they give you lots of options on the components.

After playing around with the "Eland Pedestal" tower server, I found that for about $1,200 I could get something like this:

  • Quad Core Intel Xeon X3230 2.66 GHz 1066 MHz FSB 8 MB L2 65nm
  • 4 GB DDR2
  • 3x 500 GB drives in a RAID 5 array

Wouldn't mind having two or three of those babies for my cluster.
The site blog for indicium.us.
Linux Projects
Online Games
Unsung Linux Games

RSS Feed

Powered by Movable Type 4.21-en
and GNU/Linux


Creative Commons License
The content of this blog is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. See my copyleft page for more details.