Skip to content

Lambda expression not working in a notebook with xeus-cpp 0.10.0 #476

@Hind-M

Description

@Hind-M

I'm trying to progress with this PR and have the example notebook work properly.
But when trying to run this snippet in a cell, the kernel simply dies without any further information:

xw::slider<double> slider2;

slider2.observe<decltype(slider2)>(
    slider2.value.name(),
        [&](auto& s) {
            std::cout << "The slider current value is " << s.value << std::endl;
        }
);

Note that this works fine when added to test/test_xwidgets.cpp:

For example (this is not pushed yet in the PR as still debugging):

TEST_CASE("slider_observe")
{
    slider<double> s;
    bool observed = false;
    double observed_value = 0.0;

    CHECK_EQ("value", s.value.name());

    s.observe<decltype(s)>(
        s.value.name(),
        [&](auto& w)
        {
            observed = true;
            observed_value = w.value;
        }
    );

    // Initial assignment should trigger observer
    s.value = 5.0;
    CHECK_EQ(true, observed);
    CHECK_EQ(5.0, observed_value);

    // Reset and try again
    observed = false;
    s.value = 10.0;
    CHECK_EQ(true, observed);
    CHECK_EQ(10.0, observed_value);
}

Note that the hack snippet below - explicitly using a std::function wrapping the lambda - is working in the notebook without any crashes or errors:

xw::slider<double> slider2;

slider2.observe(
    slider2.value.name(),
    std::function<void(xw::slider<double>&)>(
        [&](xw::slider<double>& s) {
            std::cout << "The slider current value is " << s.value << std::endl;
        }
    )
);

EDIT: The bug is most likely coming from CppInterOp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs triageUsed in auto labelling of new issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions