-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider moving Seq implementations from Seq to SeqImpl, avoiding default methods where they aren't "defaults" but "trait implementations" #158
Comments
Hey, just curious why you're wanting to avoid the default methods? Just wondering if you have a lesson learned I could pick up. |
Just a matter of style. I've realised that suddenly, most implementation has moved to the interface, rather than the backing class and that feels kind of weird for a "default" method. I realise that the title is a bit too agressive. I'm undecided yet, whether this change should be made or not. |
Cool, that makes sense. Thanks for the insight! |
@stephenh Hmm, just encountered another issue, proving my concerns to be justified: I've implemented Now, what happens to, for instance: long count(); In default long count() {
collect(Collectors.counting());
} But because
Default methods can be a very tricky thing from a backwards-compatibility point of view. It's dangerous to provide default methods, unless there's really no other way... |
Well, you could have overcome this by providing a default for |
Well, having some C# background, I treat default methods as kind of extension methods wherever possible. Here's how I see it: Pros of default methods:
Cons of default methods:
Here's what I try to use in my projects to address the above: I stick to default methods but delegate any non-trivial implementation (say, longer than 2 or 3 lines) to a static method in some (potentially package-private) utility class. For example:
In the same way, we could delegate all the many static methods in
JVM will optimize such calls anyway so there's no performance penalty but thanks to such approach, the |
I'd agree if there was the possibility to define I can say that in
I have never been a big fan of this practice of having "almost
One caveat: As of Java 8, |
Good point :)
OK, I can accept that you prefer to have all these methods in
I didn't realize that but what I meant is the inlining of static methods to which the default methods delegate. Here's an example of what I mean in case it's not entirely clear:
|
Now that I considered it again, I realized the static methods might not be inlined into the body of the default methods because of their length (these static methods may be too long). This would leave us with inlining the calls to the default methods which, as you pointed out, is not yet possible. Nevertheless, I'm still in favor of extracting many of these methods. For static methods (like |
That's what this issue is about :)
Why not just put everything in
Even better, avoid default methods and implement classic virtual methods in
Maybe I misunderstood you somewhere in the discussion. The intention of the issue is clear. Somehow, you have a very different change in mind (I believe). What's the intention of the change you have in mind? |
OK, as a foreword: I understand that the original proposal consist in moving the implementations from What I propose is to move the implementations from My intention is just that To sum up, it seems we both agree that As a result, (*) BTW: Why some of the |
No description provided.
The text was updated successfully, but these errors were encountered: