-
|
Hello, would it be possible to codegen the object conversion trait? https://book.jank-lang.org/cpp-interop/native-values.html#implementing-your-own-trait I feel like this code could be easily added in a compilation step to guarantee compatibility in case the user doesnt add: (cpp/raw "struct person
{ std::string name; };
namespace jank::runtime
{
template <>
struct convert<person>
{
static obj::keyword_ref name_kw;
static obj::persistent_hash_map_ref into_object(person const &p)
{
return obj::persistent_hash_map::create_unique(std::make_pair(name_kw, make_box(p.name)));
}
static person from_object(object_ref const o)
{
auto const name{ try_object<obj::persistent_string>(get(o, name_kw)) };
return person{ name->data };
}
};
obj::keyword_ref convert<person>::name_kw{ __rt_ctx->intern_keyword(\"name\").expect_ok() };
}")So, the user actually only needs to write: (cpp/raw "struct person
{ std::string name; };")And the rest will be codegen |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi! Thanks for the interest. C++26 will add compile-time reflection. With this, we could iterate through the members of a struct in order to recursively generate a conversion trait. However, C++26 is not yet standardized and is also not yet supported by any major compilers. So, in the future, I think we can automate this. Until then, the only way would be to use a DSL, which is probable macro-based. That's not something I'm interested in hacking together. Conversion traits are super helpful for primitive types, but there is a performance risk of building your program around them for more complex types. There is also a codegen bloat concern for automating this process for every C++ type, so it would need to be opt-in either way. |
Beta Was this translation helpful? Give feedback.
Hi! Thanks for the interest.
C++26 will add compile-time reflection. With this, we could iterate through the members of a struct in order to recursively generate a conversion trait. However, C++26 is not yet standardized and is also not yet supported by any major compilers.
So, in the future, I think we can automate this. Until then, the only way would be to use a DSL, which is probable macro-based. That's not something I'm interested in hacking together.
Conversion traits are super helpful for primitive types, but there is a performance risk of building your program around them for more complex types. There is also a codegen bloat concern for automating this process for every C++ type, s…