76 points · by Jarlakxen · 12y ago · 90 comments
interface IOConsumer<T> extends Consumer<T> {
@Override
default void accept(T t) {
try {
maybeAccept(t);
} catch (IOException ie) {
throw new RuntimeException(ie); }
}
void maybeAccept(T t) throws IOException;
}
As IOConsumer<T> is also a Consumer<T>, if you can assign the lambda to an IOConsumer<T> it will get this wrapping behaviour automatically. So your code might look something like: try (Writer writer = new StringWriter()) {
final IOConsumer<String> ioPrinter = writer::write;
// hand ioPrinter off to something that expects a Consumer<String>
}
This is by no means beautiful; for 'writer::write' to be treated as an IOConsumer<T> requires that the target type be IOConsumer<T>, so typically would require an interim assignment. It does allow us to simplify the 'wrapping' method, though, so that lambdas can again be used as one-liners: static <T> Consumer<T> maybe(IOConsumer<T> consumer) {
return consumer;
}
try (Writer writer = new StringWriter()) {
final Consumer<String> printer = maybe(writer::write);
}
It would be nice not to have these wrinkles, but I think Java 8 has done a fairly decent job of preserving backwards compatibility while introducing language features and APIs which feel like a vast improvement. I'd love to see reified generics in the future, particularly if the type parameter can be extended to support primitives. interface ConsumerE<T, E extends Exception> {
void accept(T t) throws E;
}
This actually works! The type inference is good enough to figure out the actual type of E.Note that Java 8 also includes specialized wrappers like RuntimeIOException. So it might be nice to extend your example:
static <T, E extends Exception> Consumer<T> wrapped(
Function<? super E, RuntimeException> wrapper,
ConsumerE<T, E> consumer) {
return (t) -> {
try {
consumer.accept(t);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw wrapper.apply((E) e);
}
};
}
Used as follows: Consumer<String> printer = wrapped(RuntimeIOException::new, writer::write);> but for such a large code-base I think building dependencies from source ... is probably not a bad idea.
That's a bad idea. You want to manage as little code as possible, especially given the already large codebase. It's certainly doable, but in the end you realize the benefit is just not worth the effort.
Of course, that was just a symptom of a far larger problem: It always seemed that when faced with the dilemma of favoring some PL research goal vs. real working-man's use, Scala always chose the former; it was like its real (though not stated) target audience were PL PhDs. It seems the winds have shifted recently, but probably too late, and Scala is already weighed down by too many "PhD" features that need to be removed in a way that would severely break source compatibility.
Say what you want about Java, it's really great for big projects you must depend on and need to maintain for many years.
So I am not the one bringing new technologies into the project, if the customer didn't explicitly ask for them, even if I enjoy playing around with them.
That said, lack of algebraic types and other functional language features in C# is starting to have the same effect.
The day I'm not bringing new tech that no one asked for into my work is the day I either die or quit programming :)
Unfortunately that's a big stop sign for me to pursue .NET knowledge/career.
As for bringing new tech, I also do it, but not on the typical enterprise consulting projects where I spend most of my working time.
On those, customers only allow for "proven" tech.
Of course I try to use appropriate technology for given requirements. But if language X lets me solve the same problem in a clearer, more expressive way than language Y, and satisfies all the requirements, why wouldn't I use language X? The customer is the business expert, I'm the technology expert - I should be collaborating in the technology choices, not relying on them to make technical suggestions.
I wouldn't advocate it for production yet, I agree. I started using it for personal projects, though, and so far its lack of maturity mostly shows in documentation and harmless quirks. Not outright bugs.
It's 100% interop ensures that you can only implement bits and pieces in Kotlin, not necessarily the entire app. So you don't have to jump into it head first and commit yourself entirely.
Personally I would use it for bussines logic where the lack of LINQ / Streams / lambdas is hurting code readability. It is also very useful for plain data objects, since it removes so much of Java's verbosity.
Scala has got more things that Kotlin, there is a comparison of features: http://confluence.jetbrains.com/display/Kotlin/Comparison+to...
But I'm already happy with the advantages Kotlin has over old Java, Android's Java. It's fancy enough.
(It still has the problems, of course, that WinForms was written back in 2002 and doesn't even use Generics, which would make the API a lot cleaner.)
- tail call optimization
- AOT compilation as part of the standard toolchain. No need to rely on third party vendors
- value types
- reifeid generics
- painless FFI with external code
EDIT: - Forgot about direct control over SIMD instructions.
All of those issues are being discussed for Java 9+ and have corresponding projects at OpenJDK.
RyuJIT seems to deliver a nice, practical boost to performance.
.NET Native lets you sidestep (with several caveats) the JIT and start executing your code much more quickly.
And every other major release they seem to tune the GC to be harder/better/faster/stronger etc.
Because I read this article, and the article from the Jooq blog he references, and I can't see anything beyond one guy's preference for one thing, and Java 8 does it the other way.
a compromise would perhaps be to allow at least reified primitive generics so that List<int> would be a proper runtime type whereas List<Object> would be the runtime version of all reference types in List<T>.
Obviously, this would be a half-assed solution unless custom value types like point structs would also be allowed without boxing in similar scenarios. I'm not sure but I think that these things need pretty significant changes to the runtime.
My conclusion is that Java doesn't need this. It won't be elegant no matter what amount of lipstick is added. I even think some of the current features like lambdas are overkill. Instead, features should be added at the vm level to allow for other jvm langs to use them. A good functional language with good generics over primitives and proper value types would be awesome. F# is a good example of such a language (scala and clojure aren't)
This is actually pretty much exactly what the .NET runtime does. All value types get a separately-JIT'd version of the type, while all reference types share the same Object-based version. See section on implementation here:
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).as...
Once it's been decided Java should get value types, generic reification became a more urgent necessity.
Something I really miss in C#/Java is a possibility to opt out of GC and use more stack allocation.