Uploaded image for project: 'Mesos'
  1. Mesos
  2. MESOS-4611

Passing a lambda to dispatch() always matches the template returning void

    XMLWordPrintableJSON

Details

    • Mesosphere Sprint 38
    • 5

    Description

      The following idiom does not currently compile:

        Future<Nothing> initialized = dispatch(pid, [] () -> Nothing {
          return Nothing();
        });
      

      This seems non-intuitive because the following template exists for dispatch:

      template <typename R>
      Future<R> dispatch(const UPID& pid, const std::function<R()>& f)
      {
        std::shared_ptr<Promise<R>> promise(new Promise<R>()); 
       
        std::shared_ptr<std::function<void(ProcessBase*)>> f_(
            new std::function<void(ProcessBase*)>(
                [=](ProcessBase*) {
                  promise->set(f());
                }));
      
        internal::dispatch(pid, f_);
        
        return promise->future();
      }     
      

      However, lambdas cannot be implicitly cast to a corresponding std::function<R()> type.
      To make this work, you have to explicitly type the lambda before passing it to dispatch.

        std::function<Nothing()> f = []() { return Nothing(); };
        Future<Nothing> initialized = dispatch(pid, f);
      

      We should add template support to allow lambdas to be passed to dispatch() without explicit typing.

      Attachments

        Activity

          People

            haosdent@gmail.com haosdent
            klueska Kevin Klues
            Michael Park Michael Park
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: