marcobambini / gravity

Gravity Programming Language
https://gravity-lang.org
MIT License
4.3k stars 229 forks source link

lambda captured #404

Open k1epic opened 1 year ago

k1epic commented 1 year ago


// c++20    vs2019 (v142) /////////////////////////////

struct test
{
    int a;

    void init ( const std::string& name, gravity_vm * vm)
    {

        gravity_class_t* class_t      = gravity_class_new_pair(NULL, name.c_str(), NULL, 0, 0);
        gravity_class_t* class_t_meta = gravity_class_get_meta(class_t);

                        // [&a]
                        // [&]
        auto _fn = [this](gravity_vm* vm, gravity_value_t* args, uint16_t nargs, uint32_t rindex)->bool
        {
            int b = a + 10;
            return true;
        };

                // NEW_CLOSURE_VALUE
            // static gravity_value_t a
            // {
        //    .isa = gravity_class_closure,
        //    .p   = (gravity_object_t*)gravity_closure_new(NULL, gravity_function_new_internal(NULL,NULL,  (_fn) ,  0))
            // };

        gravity_class_bind(class_t_meta, "fn_example", NEW_CLOSURE_VALUE(_fn));

        gravity_vm_setvalue(vm, name.c_str(), VALUE_FROM_OBJECT(class_t));
    }
};

//  error  /////////////////////////////

// error C2664: 'gravity_function_t *gravity_function_new_internal(gravity_vm *,const char *,gravity_c_internal,uint16_t)' :  
// cannot convert argument 3 from 'test::init::<lambda_1>' to 'gravity_c_internal'
// No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
marcobambini commented 1 year ago

@k1epic it is a C++ compiler-generated error and not a Gravity error. I am not sure I can help you with this issue.