Things Java Should Have

1. A new return type: this

A this method would be written like a void method, with no return parameters, and no need to explicitly call return. However when called the this method would actually return a reference to its "this" object. As such, this methods could never be static.

Consider the following example, where I'll abuse the builder pattern slightly, and use the existing String and StringBuilder classes to represent the concept of immutable and mutable strings, respectively.

public class String {
    public String append(String affix) {
        // return type "String" says that this method returns
        // a new object, which implies that this object *won't*
        // actually be modified.
    }
}

public class StringBuilder {
    public this append(String affix) {
        // return type "this" says that this method returns
        // a reference to this object, implying strongly that
        // this object *will* be modified.
    }

    public void clear() {
        // return type "void" says that this method doesn't
        // return anything, so can't be chained. Implies
        // very strongly that this object *will* be modified.
    }
}

The return type of this allows us to chain method calls (like we currently can with StringBuilder.append, which has a return type StringBuilder), but with the added advantage that just from the method signatures a coder would be able to infer certain side-effects of the methods without having to read any documentation. Currently it's not immediately apparent (although it's not hard to work out) that StringBuilder.append returns a reference to the actual StringBuilder object, not a whole new one.

2. Multiple inheritence by extending multiple abstract (or "instantiable") classes.

This one I haven't sorted out all the kinks in my mind. I'm still not entirely sure how to deal with polymorphism, but I have a feeling about what might work. Essentially, I propose the following syntax:

public class A {
    public void foo() {}
}

public class B {
    public void foo() {}
}

// variation one: explicit
public class AB extends A, B {
    public void A.foo() {
        return A.super.foo();
    }
    public void B.foo() {
        return B.super.foo();
    }
}

// variation two: almost explicit
public class AB2 extends A, B {
    public void A.foo() {
        return super.foo(); // A.super is implied since this method
    }                       //  overrides A.foo

    public void B.foo() {
        return super.foo(); // B.super is implied since this method
    }                       //  overrides B.foo
}

// variation three: implied
public class AB3 extends A, B {
    public void foo() {     // A.foo is implied as A is the first
                            //  parent in the 'extends..' list
                            //  which defines foo()
        return super.foo(); // A.super is implied since this method
    }                       //  overrides A.foo

    public void B.foo() {
        return super.foo(); // B.super is implied since this method
    }                       //  overrides B.foo
}

// variation four: misc.
public class AB4 extends A, B {
    public void blah(int n) {
        A.super.foo();      // this could be just "super.foo()", as
                            //  per variation three
        B.super.foo();
    }
}

I'm thinking that there'd have to be some fancy runtime stuff to ensure that A myA = new AB(); myA.foo(); executes the appropriate AB.A.foo() method. I really don't know how we'd deal with this:

public class A {
    public void foo() {}
}

public class B {
    public void foo() {}
}

public interface I {
    public void foo();
}

public class AB extends A, B, I {
    ...
}

I myI = new AB();
myI.foo();

Maybe defaulting to the first parent. I dunno. I haven't even mentioned disagreeing return types, either. Someone more clever than me can think of something, I'm sure.

Edit: check the addenda to this post. (2008-01-06)
... Matty /<


Matthew Kerwin

Published
Modified
License
CC BY-SA 4.0
Tags
development, software

Comments powered by Disqus