Chaining is a style that some people like and some don't. It's popular in functional languages. One place where I like to use it is when I need to create a tempory object, set a few of its properties and extract a result:
Hi Jeff. I'm familiar with chaining and I think your SelectStatement example is a great example of where it makes the code easier to read, but I just don't see how it's useful with a printf or format statement. I'm not even sure it's that useful when appending, since that could almost certainly be more cleanly implemented as a single format or printf statement.
is basically the same as writing
I realize I could also use it to flush, close or whatever, but it seems like you'd rarely want to close a file, unconditionally, right after writing to the file.
I guess I'm just trying to understand when the ability to chain format or printf would be useful.
We're binary code: a one and a zero<br />You wanted violins and you got Nero
You're right, I'd don't see the big advantage to having *some* methods allow chaining, especially why other PrintWriter methods don't. For example, you can write:
Because all the print methods have void return type. (One could argue that it's time to use printf, but not everyone likes using formating when they don't have to...)
Did you notice the cool thing happening in PrintWriter? It implements Appendable:
And when it implements these methods, it uses Java 1.5's covariant return types to change the return type of these methods to PrintWriter. (I can't think of another place in the API that they use CRT ...) The API for Appendable also states: The Appendable interface must be implemented by any class whose instances are intended to receive formatted output from a Formatter.
There is no emoticon for what I am feeling!
Is that a spider in your hair? Here, threaten it with this tiny ad: