[JAVA-3046] Deprecate MongoIterable#forEach Created: 10/Oct/18  Updated: 28/Oct/23  Resolved: 22/Oct/18

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: 3.9.0

Type: Improvement Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-2010 Support the use of Java 8 lambdas and... Closed

 Description   

The forEach(Block<? super TResult> block) method on MongoIterable conflicts with the default forEach method added to java.lang.Iterable in Java 8. If you try to use either with a lambda expression you get a compiler error which forces you to cast the lambda to either Block or Consumer, which is not intuitive.

The default forEach method in Iterable, though, is dangerous to use with MongoIterable because it's not possible for it to force the MongoCursor to be closed in the case where Consumer.accept throws an exception, but in scope of JAVA-2010 we plan to address that. Given that, it makes sense to deprecate MongoIterable#forEach(Block<? super TResult> block) so that it can be removed in the next major release and users will stop getting the compiler error.



 Comments   
Comment by Githook User [ 22/Oct/18 ]

Author:

{'name': 'Jeff Yemin', 'email': 'jeff.yemin@10gen.com', 'username': 'jyemin'}

Message: Deprecate MongoIterable#forEach(Block<? super TResult> block)

With the addition of the default method Iterable#forEach(Consumer)} in
Java 8, MongoIterable#forEach(Block<? super TResult> block) results in
an error at the point of call of forEach when using a lambda, which can
only be resolved by casting to either Block or Consumer. Rather than
keep that unfriendly API, we deprecate MongoIterable#forEach so that
it can be removed in the next major release of the driver (which will
required Java 8).

JAVA-3046
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/690dc0273a0463771030b076803efbdb4f38aa94

Generated at Thu Feb 08 08:58:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.