diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 72e82dd1..b7fe4dfd 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -922,6 +922,20 @@ namespace chaiscript { throw exception::eval_error("Type alias redefined '" + e.name() + "'"); } + dispatch::Param_Types to_underlying_param_types(std::vector>{ + {new_type_name, user_type()}}); + + auto to_underlying_body = dispatch::make_dynamic_proxy_function( + [](const Function_Params &t_params) -> Boxed_Value { + const auto *obj = static_cast(t_params[0].get_const_ptr()); + return obj->get_attr("__value"); + }, + 1, + std::shared_ptr(), + to_underlying_param_types); + + t_ss->add(to_underlying_body, "to_underlying"); + return void_var(); } }; diff --git a/unittests/strong_typedef.chai b/unittests/strong_typedef.chai index 305c6792..aa75b711 100644 --- a/unittests/strong_typedef.chai +++ b/unittests/strong_typedef.chai @@ -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)