RxCpp icon indicating copy to clipboard operation
RxCpp copied to clipboard

Resource usage

Open marco6 opened this issue 5 years ago • 1 comments

I have problems understanding how to use a resource to create a new observable from scratch. The most trivial example I can come out with is:

rxcpp::observable<>::scope(
	[]() { return rxcpp::resource<int>(1); },
	[](rxcpp::resource<int> res) { 
		return rxcpp::observable<>::create<int>(
			[res](rxcpp::subscriber<int> sub) {
				sub.on_next(res.get());
				sub.on_completed();
			}
		); 
	}
);

In this case, I would get compilation error as resource::get is not const and my lambda is not mutable. Changing the lambda to mutable leads to #473 . Am I misusing observable::scope? I could not find many information about its correct usage...

Still, to make my code work, I could make mutable the callback in observer as proposed in #473, but I'm wondering if it does makes sense in this case. Is there a compelling reason not to make resource::get() const? By looking at the source I can see that it is just calling get on a shared_ptr (which is const already...).

marco6 avatar Jun 15 '19 08:06 marco6

yes, resource::get should be const. at some point I would like to revisit the design of scope as well

kirkshoop avatar Jun 22 '19 18:06 kirkshoop