L
Lew
Wojtek said:Peter Duniho wrote :Robert Dodier wrote:
Anonymous classes avoid clutter, why not
anonymous methods as well.
Because of the implementation of the JVM and the strong static typing
nature of Java makes first-class methods tricky to implement.
The first point (implementation of JVM) might be valid. The second
(strong static typing) certainly is not. Other strongly typed
languages have closures, without any real tricky aspects at all.
[...]
The developers of Java decided that interface-based programming was
much cleaner than function pointer-based programming, and from a
maintainability perspective, I think most people would agree with them.
"Most" from what sample set? The one thing I find most awkward about
Java is its lack of a proper function pointer type and closures. One
of the things I love most about C# is those features. I dare say that
looking at a broader set of people, you'd find that many, if not most,
would agree that one of the things holding Java back the most is its
refusal to include any functional-style paradigms.
Hmmm, function pointers...
interface Foo
{
public void doSomething();
}
class One implements Foo
{
One()
{
}
public void doSomething()
{
// stuff to be done
}
}
class Two implements Foo
{
Two()
{
}
public void doSomething()
{
// stuff to be done
}
}
enum FooHolder
{
ONE(new One()),
TWO(new Two());
private Foo foo;
FooHolder(Foo fooIn)
{
foo = fooIn;
}
public Foo getFoo()
{
return foo;
}
better:
public void doSomething()
{
this.foo.doSomething();
}
}
...
FooHolder.ONE.getFoo().doSomething();
FooHolder.ONE.doSomething();
or
work(Foo.TWO);
public void work(FooHolder foo)
{
foo.getFoo().doSomething();
foo.doSomething();
}
Off the top of my head, but the syntax is basically sound.
This is what people are calling "too verbose" and has them clamoring for
first-class functions.
(The verbosity of the holder idiom doesn't count - it'd still be there with a
real closure.)
This is a religious argument, of course. I'm not out to convert anyone, just
express an opinion.