Address review: add to_underlying function for strong typedefs

Registers a to_underlying() function for each strong typedef that
returns the wrapped base value from the Dynamic_Object's __value attr.

Requested by @lefticus in PR #680 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
leftibot 2026-04-13 18:18:37 -06:00
parent ef7c338452
commit 61cbf1cda1
2 changed files with 24 additions and 0 deletions

View File

@ -922,6 +922,20 @@ namespace chaiscript {
throw exception::eval_error("Type alias redefined '" + e.name() + "'");
}
dispatch::Param_Types to_underlying_param_types(std::vector<std::pair<std::string, Type_Info>>{
{new_type_name, user_type<dispatch::Dynamic_Object>()}});
auto to_underlying_body = dispatch::make_dynamic_proxy_function(
[](const Function_Params &t_params) -> Boxed_Value {
const auto *obj = static_cast<const dispatch::Dynamic_Object *>(t_params[0].get_const_ptr());
return obj->get_attr("__value");
},
1,
std::shared_ptr<AST_Node>(),
to_underlying_param_types);
t_ss->add(to_underlying_body, "to_underlying");
return void_var();
}
};

View File

@ -43,3 +43,13 @@ try {
} catch(e) {
// Expected: Seconds is not Meters
}
// to_underlying should return the base value
assert_equal(to_underlying(m), 42)
assert_equal(to_underlying(s), 10)
// to_underlying result should be a plain value, not a strong typedef
def takes_int(int i) {
return i
}
assert_equal(takes_int(to_underlying(m)), 42)