Stewart Stremler wrote:
>begin quoting Christopher Smith as of Tue, May 23, 2006 at 12:57:20PM -0700:
>
>
>>>I have not found it hard to use at all. In fact, I've found it much
>>>easier than C++.
>>>
>>>
>>Java's generics are much easier to use than C++'s templates. They are,
>>unfortunately, quite limited.
>>
>>
>
>Still too much like C++'s templates, I feel.
>
>Can't say I could've come up with anything clearer myself, alas. I
>fear that it may be the cleanest way to do what's being done.
>
>
Once you grok type theory C++ templates and Java Generics become a bit
clearer, although there really is no overcoming C++'s baroque syntax.
>>If you can show me how to do policy based resource management in Java
>>(i.e. no "finally" blocks in the application code), I might agree with
>>you. Otherwise, arguments that Java is "better" than C++ are kind of
>>irrelevant to whether Java has this capability.
>>
>>
>
>What do you mean by "policy based resource management" here?
>
>Not using try...finally in Java would be like requiring allocation of
>objects on the stack and not using destructors or copy constructors
>in C++. Some features are there for a reason, not added as an
>afterthought.
>
>
All features are there for a reason. Agreed. Sometimes the reasons a
feature isn't there are lousy (read up on why an anonymous inner class
can't access non-final variables declared inside its enclosing method
and you'll want to cry). That said, C#'s "using" statement provides a
simple example of how to get the basics of this in place:
using (TextWriter w = File.CreateText("some.txt"))
{
// do stuff
}
The nice thing here is that you don't have to skip to the end of the
block to know that 'w' will have Dispose() invoked on it (well, so long
as it doesn't become null). At one point I wrote myself a little
preprocessor that used the overloaded modifier "transient" to achieve
something a bit nicer with Java. Basically it was triggered by using the
transient on a local variable so:
void foo() {
transient FileInputStream input = openFile();
transient SocketOutputStream output = connect().getOutputStream();
// do stuff
}
became:
void foo() {
FileInputStream input = openFile();
try {
SocketOutputStream output = connect().getOutputStream();
try {
//do stuff
} finally {
output.finalize();
}
} finally {
input.finalize();
}
}
Now you have the basics there, only you are limited to a single policy
(essentially finalize and pray). With annotations you could go hog wild
with this and achieve something which matched all the "Java compatible"
concepts from what I'm talking about.... only attributes just are
strangely limited in Java. It'd be nice to do something like:
void foo() {
// finalize this file immediately when it drops out of lexical scope
@LexicalResource FileInputStream input = openFile();
// finalize this socket as soon as it stops being reachable by anyone and
// try doing shutdownOutput() if finalize() throws an exception
@SharedResource (failurePolicy @TryShutdownOutput) socket = getSocket();
//do stuff
}
But the limitations of Java's Annotations would require a custom
annotation processing tool to make it work (and you certainly couldn't
do it dynamically at runtime, which would have been nice).
--Chris
--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg