@@ -129,27 +129,25 @@ struct value_t {
129129 // Note: only for debugging and error reporting purposes
130130 virtual std::string type () const { return " " ; }
131131
132- virtual int64_t as_int () const { throw std::runtime_error ( type () + " is not an int value" ); }
133- virtual double as_float () const { throw std::runtime_error ( type () + " is not a float value" ); }
134- virtual string as_string () const { throw std::runtime_error ( type () + " is not a string value" ); }
135- virtual bool as_bool () const { throw std::runtime_error ( type () + " is not a bool value" ); }
136- virtual const std::vector<value> & as_array () const { throw std::runtime_error ( type () + " is not an array value" ); }
137- virtual const std::vector<std::pair<value, value>> & as_ordered_object () const { throw std::runtime_error ( type () + " is not an object value" ); }
138- virtual value invoke (const func_args &) const { throw std::runtime_error ( type () + " is not a function value" ); }
132+ virtual int64_t as_int () const { throw_type_error ( " is not an int value" ); }
133+ virtual double as_float () const { throw_type_error ( " is not a float value" ); }
134+ virtual string as_string () const { throw_type_error ( " is not a string value" ); }
135+ virtual bool as_bool () const { throw_type_error ( " is not a bool value" ); }
136+ virtual const std::vector<value> & as_array () const { throw_type_error ( " is not an array value" ); }
137+ virtual const std::vector<std::pair<value, value>> & as_ordered_object () const { throw_type_error ( " is not an object value" ); }
138+ virtual value invoke (const func_args &) const { throw_type_error ( " is not a function value" ); }
139139 virtual bool is_none () const { return false ; }
140140 virtual bool is_undefined () const { return false ; }
141- virtual const func_builtins & get_builtins () const {
142- throw std::runtime_error (" No builtins available for type " + type ());
143- }
141+ virtual const func_builtins & get_builtins () const { throw_type_error (" has no builtins" ); }
144142
145- virtual bool has_key (const value &) { throw std::runtime_error ( type () + " is not an object value" ); }
146- virtual void insert (const value & /* key */ , const value & /* val */ ) { throw std::runtime_error ( type () + " is not an object value" ); }
147- virtual value & at (const value & /* key */ , value & /* default_val */ ) { throw std::runtime_error ( type () + " is not an object value" ); }
148- virtual value & at (const value & /* key */ ) { throw std::runtime_error ( type () + " is not an object value" ); }
149- virtual value & at (const std::string & /* key */ , value & /* default_val */ ) { throw std::runtime_error ( type () + " is not an object value" ); }
150- virtual value & at (const std::string & /* key */ ) { throw std::runtime_error ( type () + " is not an object value" ); }
151- virtual value & at (int64_t /* idx */ , value & /* default_val */ ) { throw std::runtime_error ( type () + " is not an array value" ); }
152- virtual value & at (int64_t /* idx */ ) { throw std::runtime_error ( type () + " is not an array value" ); }
143+ virtual bool has_key (const value &) { throw_type_error ( " is not an object value" ); }
144+ virtual void insert (const value & /* key */ , const value & /* val */ ) { throw_type_error ( " is not an object value" ); }
145+ virtual value & at (const value & /* key */ , value & /* default_val */ ) { throw_type_error ( " is not an object value" ); }
146+ virtual value & at (const value & /* key */ ) { throw_type_error ( " is not an object value" ); }
147+ virtual value & at (const std::string & /* key */ , value & /* default_val */ ) { throw_type_error ( " is not an object value" ); }
148+ virtual value & at (const std::string & /* key */ ) { throw_type_error ( " is not an object value" ); }
149+ virtual value & at (int64_t /* idx */ , value & /* default_val */ ) { throw_type_error ( " is not an array value" ); }
150+ virtual value & at (int64_t /* idx */ ) { throw_type_error ( " is not an array value" ); }
153151
154152 virtual bool is_numeric () const { return false ; }
155153 virtual bool is_hashable () const { return false ; }
@@ -163,6 +161,11 @@ struct value_t {
163161 // Note: only for debugging purposes
164162 virtual std::string as_repr () const { return as_string ().str (); }
165163
164+ private:
165+ [[noreturn]] void throw_type_error (const char * expected) const {
166+ throw std::runtime_error (type () + " " + expected);
167+ }
168+
166169protected:
167170 virtual bool equivalent (const value_t &) const = 0;
168171 virtual bool nonequal (const value_t & other) const { return !equivalent (other); }
0 commit comments