From 61cbf1cda1b954748d7d46b363cd23402657568b Mon Sep 17 00:00:00 2001 From: leftibot Date: Mon, 13 Apr 2026 18:18:37 -0600 Subject: [PATCH] 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) --- include/chaiscript/language/chaiscript_eval.hpp | 14 ++++++++++++++ unittests/strong_typedef.chai | 10 ++++++++++ 2 files changed, 24 insertions(+) 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)