Skip to content

Instantly share code, notes, and snippets.

@graninas
Last active January 25, 2019 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graninas/d22094192a1b8261f68aa409c7932ee2 to your computer and use it in GitHub Desktop.
Save graninas/d22094192a1b8261f68aa409c7932ee2 to your computer and use it in GitHub Desktop.
C++ FP Limitations

Weak type deduction

auto, lambdas -> std::function

auto m1 = newTVar(10);                                                // ok
auto m2 = stm::bind<TVarInt, TVarInt>(m1, [](const auto& tvar)        // ok, full bind type specified
{
    auto mm1 = writeTVar(tvar, 20);
    return sequence(mm1, pure(tvar));
});
auto m3 = stm::bind(m2, mReadTVar);                                   // fail
error: no matching function for call to ‘bind(stm::church::STML<stm::TVar<int> >&, stm::church::<lambda(const auto:7&)>&)’
     return bind(ma, f);
            ~~~~~^~~~~~~
candidate: template<class A, class B> stm::church::STML<B> stm::church::bind(stm::church::STML<A>, std::function<stm::church::STML<B>(A)>)
 STML<B> bind(STML<A> ma,
         ^~~~~
note:   template argument deduction/substitution failed:
note:   ‘stm::church::<lambda(const auto:7&)>’ is not derived from ‘std::function<stm::church::STML<B>(A)>’
                    return bind(ma, f);
                           ~~~~~^~~~~~~

constexpr error handling

constexpr int ediv (int a, int b)
{
    return b == 0 
        ?                   // WTF??
        : a / b;
}

Lambdas are of different types

auto lam1()
{
    return true
        ? [](){ return static_cast<int>(0); }   // ok (casted to int(*)())
        : [](){ return static_cast<int>(0); };
}

auto lam2()
{
    return true
        ? [=](){ return static_cast<int>(0); }   // compilation error, casted to ???
        : [=](){ return static_cast<int>(0); };
}

// Solution:
auto lam3()
{
    return [=]()
        { return 
            true
                ? static_cast<int>(0)
                : static_cast<int>(0);
        };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment