Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Three Java Variants Extend the Language : Page 3

Learn about three of the most promising Java variants—how they work, what their features do, and how you can integrate them into your development environment.


advertisement
The MultiJava Variant
The MultiJava package is from Iowa State University. MultiJava adds two features:
  • Symmetric multiple dispatch — Symmetric multiple dispatch lets you specify several different implementations for a particular method. The implementations depend on the dynamic types of that methods arguments.

  • Open classes — Open classes are classes that you can add methods to without actually modifying the original source code.

  • Symmetric Multiple Dispatch
    Symmetric multiple dispatch allows you to provide multiple implementations of a method. The choice of which one to use depends on the run-time type of the arguments to that method. For example, consider two variants of a method called show(). The idea here is that when you show a string, you want to put quotes around it. So you declare a special show() method just for strings, which puts quotes around the object's representation:

    public void show( Object o ) { System.out.println( o ); } public void show( Object@String s ) { System.out.println( "\""+s+"\"" ); }



    Note the syntax change—the second variant has an argument of type Object@String, which isn't a regular Java type. This type specifier says: use this variant if the object that is being passed in is actually a String. Sounds a lot like method overloading, doesnt it? For example, in regular Java you easily could write this:

    public void show( Object o ) { System.out.println( o ); } public void show( String s ) { System.out.println( "\""+s+"\"" ); }

    With MultiJava, you can use one or the other, depending on how you declare your object:

    Object o = new Object(); show( o ); // uses show( Object ) String s = "s"; show( s ); // uses show( String )

    So how does symmetric multiple dispatch differ? Well, what if you declare your string argument as follows:

    Object o = "really a string"; show( o );

    If you're using regular method overloading, this will use show( Object ). If you're using multiple dispatch, this uses show( String )—which is tricky. In the declaration above, the object you're creating is a String object, but it's being declared as an Object. This is valid, of course, since String is a subclass of Object (as are all objects in Java).

    But when you pass this object—which is really a string—to some method, which method do you use? Is it an Object or a String? If you use regular method overloading, then you make the choice based on the type of the variable, not the value. This is how overloading works in Java, as well as in C++ and many other languages with overloading. It's kind of a subtle point, but suffice it to say that if you've done programming in a popular object-oriented language, this is what you're used to.

    With symmetric multiple dispatch, the situation is reversed: the choice of method is made based on the type of the value, not the variable. Thus, the choice is necessarily made at run-time. More precisely, the argument type static@dynamic declares that the method is passed values of type dynamic contained in variables of type static. If a particular variable has no exact match for a particular value, then the closest-matching superclass is used, if any.

    It should be clear why this is useful. It lets you specify multiple implementations of a method, and the system will choose the right one based on the run-time value of the variable. This is particularly useful when subclassing, because it lets you add special custom behavior for a particular set of arguments and you don't have to modify the superclass. (See the MultiJava Web site for more examples of the value of Symmetric Multiple Dispatch.)

    The term multiple in symmetric multiple dispatch comes from the fact that the value of the argument is used to execute, or dispatch, the method call. Normally, the object that owns the method is the only one that gets to decide (at run-time) which method to use. With multiple dispatch, the arguments are used as well.

    Open Classes
    Open classes are classes to which you can freely add new methods. Normally, if you want to add a method to a class, you have to modify the source code for that class. With open classes, you don't. Adding a method to a class is as easy as this:

    public void SomeClass.method() { // ... }

    This line adds a method called method to the class SomeClass. The method can be used just like a regular method:

    SomeClass sc = new SomeClass(); sc.method();

    In regular Java, you could not do this without actually modifying the code for SomeClass. MultiJava makes it possible to add such methods after the fact. You can use this facility to improve the Show example from the previous section. Remember you created a method called show, which displays an object on System.out. Instead of using a separate method, why not add the method to the object itself so it knows how to show itself? You can do it like this:

    public void Object.show() { Show.getInstance().show( this ); }

    Note that you've added a method to Object. This is very powerful. Since every class is a subclass of Object, adding a method to Object is really adding a method to every class in the system.

    Now, you can show your objects like this:

    Object o = new Object(); o.show(); // uses show( Object ) String s = "s"; s.show(); // uses show( String )

    You'll find the complete listing for Show.pizza in Listing 4. Click here for complete instructions for running the MultiJava variant.



    Comment and Contribute

     

     

     

     

     


    (Maximum characters: 1200). You have 1200 characters left.

     

     

    Sitemap