|
Let's start with Java's object system, and add state and implementation to interfaces. This way you can write something like: role Eq {
requires 'equal_to';
method not_equal_to($a: $b) { return !$a->equal_to($b) }
}
The technique of composing classes from parts has recently been popularized by Smalltalk, Perl 6, Perl 5 (Moose), and Scala. (Scala's implementation is intentionally limited for some reason; look to Moose for the most reliable implementation that people actually use for Real Work.)If you want to do this in Java, you would have to create a class like: class NotEq {
private Eq a;
method not_equal_to(b) { !a.equals(b) }
}
class Foo implements Eq {
private NotEq neq;
Foo() { neq = new NotEq(this) }
method equal_to(b) { ... }
method not_equal_to(b) {
neq.not_equal_to(b)
}
}
This is a lot of code to write, which is why people just cut-n-paste instead.Also, Java is not merely 5 years behind, so it is worthwhile to look back farther to find ideas that Java could embrace. Java would do well to steal a sane object system like CLOS or Moose; it could really eliminate a lot of boilerplate. (One thing I think is annoying about Java is how much work the constructor has to do. In Lisp and Perl, I never write my own constructors, the object system does it for me. This means that things can be composed without the programmer having to know every detail, including which position each attribute initializer should be in.) Anyway, if you really think Java is the state of the art, you should look around a bit more. Yes, it's silly that people want to do their own type checking in "dynamic languages", but that's not why they use them -- it's all the other features that they want. The lack of static type checking is an unfortunate overreaction to C and C++. |