[JAVA-4950] Consider whether it's safe to inherit from kotlinx.coroutines.flow.Flow Created: 27/Apr/23  Updated: 28/Oct/23  Resolved: 13/Jun/23

Status: Closed
Project: Java Driver
Component/s: Kotlin
Affects Version/s: None
Fix Version/s: 4.10.0

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

Issue Links:
Documented
Related
Documentation Changes: Needed
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

The kotlinx.coroutines.flow.Flow documentation states

  • ### Not stable for inheritance
    *
  • *The `Flow` interface is not stable for inheritance in 3rd party libraries*, as new methods
  • might be added to this interface in the future, but is stable for use.

Consider whether it's safe for the Kotlin driver to inherit from it, as it does now, e.g.

public class AggregateFlow<T : Any>(private val wrapped: AggregatePublisher<T>) : Flow<T> {



 Comments   
Comment by Githook User [ 13/Jun/23 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross@mongodb.com', 'username': 'rozza'}

Message: Kotlin coroutine update. (#1135)

As the Flow interface is not stable for inheritance in 3rd party libraries,
use delegation instead.

JAVA-4950
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/019d7374f7ba4a4def1a3783437bed5a38f533fc

Comment by Ashni Mehta [ 04/May/23 ]

An update from Julien:

I agree that Flow should not be used directly as mentioned in the doc.The doc says it should not be used "as new methods might be added to this interface in the future"So a workaround would be to use AbstractFlow (https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-abstract-flow/) instead of Flow. We can also add a MongoAbstractFlow that would allow  the driver to add additional methods if AbstractFlow is not updated by the coroutine lib (which is unlikely).An other option would be to use kotlin delegation:
class AggregateFlow<T : Any>(private val wrapped: AggregatePublisher<T>) : Flow<T> by wrapped.asFlow()
Then you have no risk anymore.The second option is more elegant I would say. I don't see at this time any issue with the delegation solution.

 

Generated at Thu Feb 08 09:03:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.