ReactiveX / RxCpp

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

[WIP] Try to provide ability to use mutable/immutable lambdas dynamically #563

Closed victimsnino closed 2 years ago

victimsnino commented 2 years ago

To continue thread from #562 .

I've tried to provide override T& for on_next. From the point of actual execution it works as expected, but compiler tries to satisfy all possible (and unused) ways of execution (for example, constT& to T&). Can we solve it?

victimsnino commented 2 years ago

@kirkshoop , moved discussion to this thread as requested

kirkshoop commented 2 years ago

One option is to make the virtual function implementations forward to non-virtual internal functions that use enable-if to prune the branches that are invalid and 'magic' the ones that are invalid - const T& -> T& would make a copy, but would prefer a T&& overload of the destination on_next over a T& overload.

Another approach is to make the exact type of T supplied to the virtual_observer the only virtual overload for on_next and fail at compile time if the observer passed to the virtual_observer cannot call on_next with T.

I prefer the second option.

victimsnino commented 2 years ago

@kirkshoop, To clarify idea: Do you want to make type deduction/usage more strict? for example,

auto root = rxcpp::observable<>::just(1); // observable<int>;
auto modified = root.map([](int& v){v += 1; return v;}); // observable<int>, but internal observer int&. compilable due to int convetible to int& . Modifies original value inside "just" in this case or just sends copy everytime?
auto ref_modified = modified.map([](int& v)-> int& {return v;}); // observable<int&> Observer int&. 

ref_modified.map([](int v){}); // copy of int&
auto const_ref_modified = modified.map([](int& v)-> const int& {return v;}); // observable<const int&>
const_ref_modified.map([](int& v){}); // non compilable due to const int& non-convertible to int& 

and etc?

kirkshoop commented 2 years ago

Do you want to make type deduction/usage more strict?

@victimsnino, yep, those examples look good to me