Skip to content

Conversation

Vipul-Cariappa
Copy link
Collaborator

@Vipul-Cariappa Vipul-Cariappa commented Jul 30, 2025

WrapLambdaFromVariable: wraps a lambda variable to std::function This is required for codegen and execution, because lambda are types that cannot be expressed. Without this conversion from lambda to std::function we will not have sufficient type information to execute the lambda. Example of conversion:

// from
auto f = [](){ return 1; }
// to
namespace __cppyy_internal_wrap_g {
std::function f = ::f;
}

AdaptFunctionForLambdaReturn: This wraps a function that returns a lambda to return std::function Example:

// from
auto f() { return [](){ return 1; }
// to
namespace __cppyy_internal_wrap_g {
auto wrapper_fn(...args) {
  return std::function(f(args...));
}
}

Depends on compiler-research/CppInterOp#678

Copy link
Collaborator

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wished for a less code generating approach but I think this is good enough. Maybe @wlav will have comments.

@wlav
Copy link

wlav commented Jul 30, 2025

Not sure what you're asking to review? This is largely how I wrote it (https://github.com/wlav/cppyy-backend/blob/master/clingwrapper/src/clingwrapper.cxx#L1808), just now using auto to structure the template and apparently moved to be a part of the public API of clingwrapper? I don't see why it needs to be externally accessible, but otherwise it was the best solution I was able to come up with given that lambda is a compiler-specific type that wasn't expressed as some faux C++ type (like e.g. initializer_list is), so reflection isn't really possible.

@vgvassilev
Copy link
Collaborator

Not sure what you're asking to review? This is largely how I wrote it (https://github.com/wlav/cppyy-backend/blob/master/clingwrapper/src/clingwrapper.cxx#L1808), just now using auto to structure the template and apparently moved to be a part of the public API of clingwrapper? I don't see why it needs to be externally accessible, but otherwise it was the best solution I was able to come up with given that lambda is a compiler-specific type that wasn't expressed as some faux C++ type (like e.g. initializer_list is), so reflection isn't really possible.

Ok, thanks for the clarification. Let’s move forward then.

`WrapLambdaFromVariable`: wraps a lambda variable to `std::function`
This is required for codegen and execution, because lambda are types that cannot be expressed.
Without this conversion from lambda to std::function we will not have sufficient type information to execute the lambda.
Example of conversion:
```c++
// from
auto f = [](){ return 1; }
// to
namespace __cppyy_internal_wrap_g {
std::function f = ::f;
}
```

`AdaptFunctionForLambdaReturn`: This wraps a function that returns a lambda to return `std::function`
Example:
```c++
// from
auto f() { return [](){ return 1; }
// to
namespace __cppyy_internal_wrap_g {
auto wrapper_fn(...args) {
  return std::function(f(args...));
}
}
```
@Vipul-Cariappa Vipul-Cariappa merged commit e63b71d into compiler-research:master Aug 1, 2025
30 of 31 checks passed
@Vipul-Cariappa Vipul-Cariappa deleted the dev/Lambda branch August 1, 2025 09:18
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.

3 participants