Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation bug, create needs a template parameter. #386

Open
kirkshoop opened this issue Jul 11, 2017 · 4 comments
Open

documentation bug, create needs a template parameter. #386

kirkshoop opened this issue Jul 11, 2017 · 4 comments

Comments

@kirkshoop
Copy link
Member

On the Gitter channel -
Peter Majchrak @petoknm Jul 09 09:54
i was missing a template parameter for create

auto ints = rxcpp::observable<>::create<int>( [](rxcpp::subscriber<int> s){ s.on_next(1); s.on_next(2); s.on_completed(); });

please update http://reactive-extensions.github.io/RxCpp/ to reflect that. thanks

@petoknm
Copy link
Contributor

petoknm commented Jul 18, 2017

I think the template parameter can be inferred by the type of the lambda passed in... I see it being too verbose having a template parameter on create and on subscriber as well if they always match... I think that the way the documentation had it (without the template parameters) was cleaner and nicer

@kirkshoop
Copy link
Member Author

Yes, that is true when using the type-forgetting subscriber<T>. Perhaps a different create overload could be added for that, but then create would have a different usage depending on the type of the parameter declared in the lambda. That seems too subtle to communicate to users.

Thoughts?

@petoknm
Copy link
Contributor

petoknm commented Jul 21, 2017

What exactly do you mean by "type forgetting subscriber<T>"? and what exactly would the difference in usage be?

@kirkshoop
Copy link
Member Author

std::function<> is type-forgetting. It uses virtual functions and heap allocation to hide the type of the function that was passed to its constructor. This allows it to be used as a parameter or struct member that can hold a variety of function implementations; lambda, object, function ptr, etc..

subscriber<T, Implementation> is the full type of subscriber. This can be efficient, because no heap alloc or virtual functions are required. When subscriber<T> is used the Implementation uses virtual functions and heap alloc to hide the real Implementation type.

type-forgetting is expensive and should be used only when needed.

If a create overload was added to remove the template param when using type-forgetting it would look like this.

auto ints = rxcpp::observable<>::create( [](rxcpp::subscriber<int> s){ s.on_next(1); s.on_next(2); s.on_completed(); });

However, the existing overload would still be needed when the type is not forgotten.

auto ints = rxcpp::observable<>::create<int>( [](auto s){ s.on_next(1); s.on_next(2); s.on_completed(); });

There is no way that create could extract int from the auto param of the lambda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants