March 14th 2010 09:05 am
But Java /can/ do that!
I recently attended a talk at FOSDEM by Miguel de Icaza, a fellow enemy of “Free” software.
He, like most .NET users, is desperately seeking features to differentiate his second-rate platform from the wonderfulnessness of Java.
He showed .NET expressions, a wonderful feature whereby the actual nature of a predicate can be retrieved at runtime and optimal, say, SQL can be built to match it.
He, however, claimed that Java lacks this feature. This is not the case.
ExpressionTest shows various uses of this in Java; basically:
Expression.toSQL(new Predicate
will return:
(a = 7)
Obviously this remains slightly more verbose while the Java lambda proposals are finalised, but the feature is hardly missing!
Similarily,
return (t.a == 7 || t.a == 8 || t.a == 9) && "pony".equals(t.b);
becomes:
(a = 7 AND b = 'pony') OR
(a = 8 AND b = 'pony') OR
(a = 9 AND b = 'pony')
And, etc.
The implementation, if the devil compels you to look.
4 Comments »
Anonymous on 14 Mar 2010 at 5:59 pm #
You are a douche
3/15/2010 Update « Go Code on 15 Mar 2010 at 5:12 am #
[...] Faux's Blog But Java /can/ do that! [...]
Rick on 19 Mar 2010 at 6:32 pm #
Haha..another bitter Java developer…love it.
Miguel on 24 Mar 2010 at 7:59 pm #
Your sample requires the consumer to decompile the java code at runtime, it is a passable hack, but it is not a language feature.
This for example breaks in the following cases:
* No JVM bytecode available at runtime (for example Dalvik, or static compilers).
* Expressions being more complicated than the decompiler can handle.
* Difference in compiler output that could confuse the decompiler.
* Obfuscated JAR files that would confuse the decompiler.
By the same token the same can be also done with native x86 code. It merely requires someone to write the decompiler (LLVM has one that could be reused) but in neither case this is a language feature: it is a clever class library feature.
In the C# case we have a stable API that describes the expression tree that compilers must produce in the presence of this kind of expression.