On 04/11/2020 14:18, Justin Dekeyser wrote:
Hello everyone,
I have been following this mailing list for several months, and
earlier today my attention was drawn to
https://bugs.openjdk.java.net/browse/JDK-8140283. Actually I've been
dreaming of such a feature for a long time now.
I would really be interested in solving it, but I do not know its
current state nor if someone would agree to sponsor my work on that.
It would be my very first intervention in the Java code base.
(Still have to make sure the Oracle agreement paper does not conflict
with my current job contract, so nothing's ready for now.)
Thank you for your time,
Best regards,
Justin Dekeyser
I'd like this feature as well, but why stop at Stream? String already
has the transform method, but StringBuilder (and StringBuffer) could
also use it.
And that's where you're likely to start copy pasting. I've done so for
several builder classes I've written for myself. So here's a thought:
why add this method to classes, when you can create a trait using an
interface with a default method?
public interface Transformable<T> {
default <R> R transform(Function<? super T, ? extends R> f) {
// note: this would need documentation that a class X is
// only allowed to implement Transformable<X>
return f.apply((T) this);
}
}
So you could get the following, and each would automatically get the
transform method:
* public class String implements Transformable<String>
* public class StringBuilder implements Transformable<StringBuilder>
* public class StringBuffer implements Transformable<StringBuffer>
* public interface Stream<T> implements Transformable<Stream<T>>