diff --git a/include/zecalculator/math_objects/decl/dyn_math_object.h b/include/zecalculator/math_objects/decl/dyn_math_object.h index 0e19ff1..f252b90 100644 --- a/include/zecalculator/math_objects/decl/dyn_math_object.h +++ b/include/zecalculator/math_objects/decl/dyn_math_object.h @@ -73,7 +73,8 @@ class DynMathObject: public tl::expected, Error> requires (std::is_convertible_v and ...) tl::expected evaluate(DBL... val) const; - /// @brief returns the name of the object, if it's valid, otherwise empty string + /// @brief returns the name of the object + /// @note returns non-empty string only if this instance contains a syntactically valid equation or object std::string_view get_name() const; /// @brief gets the value of the expected (the variant) as a specific alternative diff --git a/include/zecalculator/math_objects/impl/dyn_math_object.h b/include/zecalculator/math_objects/impl/dyn_math_object.h index 4b2c333..267eca6 100644 --- a/include/zecalculator/math_objects/impl/dyn_math_object.h +++ b/include/zecalculator/math_objects/impl/dyn_math_object.h @@ -350,7 +350,9 @@ bool DynMathObject::holds() const template std::string_view DynMathObject::get_name() const { - if (bool(*this)) + if (opt_eq_object) + return opt_eq_object->name; + else if (bool(*this)) return std::visit([](const auto& val){ return val.get_name(); }, **this); else return std::string_view(); } diff --git a/test/function_test.cpp b/test/function_test.cpp index a3ee4af..345a201 100644 --- a/test/function_test.cpp +++ b/test/function_test.cpp @@ -330,4 +330,16 @@ int main() } | std::tuple{}; + "name of function in error state"_test = []() + { + constexpr parsing::Type type = std::is_same_v ? parsing::Type::FAST : parsing::Type::RPN; + + MathWorld world; + + auto& f = world.new_object() = "f( x) = cos(x) * g(X)"; + + expect(f.get_name() == "f"); + + } | std::tuple{}; + }