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

Deduce return type for all API invokable functions. #106

Merged
merged 1 commit into from
Aug 19, 2019

Conversation

accelerated
Copy link
Contributor

Signed-off-by: Alexander Damian [email protected]

Describe your changes

  • Automatically deduce the return type for all callable objects passed to the library. This means that it is not longer required/needed to specify the return type as a template parameter when when posting jobs, making it easier and more intuitive. This applies to V1 and V1 APIs including forEach and mapReduce functions.
    As an example:
 auto ctx = dispatcher.postFirst<int>([](CoroContext<int>::Ptr ctx)->int {
        return ctx->set(55); //declared type is int
    })->then<double>([](CoroContext<double>::Ptr ctx)->int {
        return ctx->set(22.33); //declared type is double
    })->then<std::string>([](CoroContext<std::string>::Ptr ctx)->int {
        return ctx->set("future"); //declared type is string
    })->then<std::list<int>>([](CoroContext<std::list<int>>::Ptr ctx)->int {
        return ctx->set(std::list<int>{1,2,3}); //declared type is list<int>
    })->end();

now becomes

auto ctx = dispatcher.postFirst([](CoroContext<int>::Ptr ctx)->int {
        return ctx->set(55); //deduced type is int
    })->then([](CoroContext<double>::Ptr ctx)->int {
        return ctx->set(22.33); //deduced type is double
    })->then([](CoroContext<std::string>::Ptr ctx)->int {
        return ctx->set("future"); //deduced type is string
    })->then([](CoroContext<std::list<int>>::Ptr ctx)->int {
        return ctx->set(std::list<int>{1,2,3}); //deduced type is list<int>
    })->end();

Notice that post(), then()... do not take template arguments anymore. Similarly for forEach or mapReduce:

    std::vector<int> start{0,1,2,3,4,5,6,7,8,9};
    std::vector<char> results = getDispatcher().forEach<char>(start.cbegin(), start.size(),
        [](VoidContextPtr, const int& val)->char {
        return 'a'+val;
    })->get();

becomes

    std::vector<int> start{0,1,2,3,4,5,6,7,8,9};
    std::vector<char> results = getDispatcher().forEach(start.cbegin(), start.size(),
        [](VoidContextPtr, const int& val)->char {
        return 'a'+val;
    })->get();

Testing performed
Compiled and ran unit tests

Additional context

  1. Note that this change is backwards compatible and no code changes are needed. Going forward, new code can simply omit the template parameters
  2. In the next major release, the template parameters marked with Deprecated may be removed to enforce simplicity, thus braking API.

@accelerated accelerated merged commit a0af3b3 into bloomberg:master Aug 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant