ReactiveX / RxCpp

Reactive Extensions for C++
Apache License 2.0
3.03k stars 390 forks source link

Resource usage #502

Open marco6 opened 5 years ago

marco6 commented 5 years ago

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 _sharedptr (which is const already...).

kirkshoop commented 5 years ago

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