minilua::Number class

Numbers in lua can be integers or floats.

The Number type automatically converts Int to Float according to the rules of lua.

The equals and hash implementation treat whole Floats and like their equivalent Ints.

Supports comparison operators and +, -, *, /, ^ (exponentiation), %, &, |.

Is hashable.

Public static variables

static static const std::string_view TYPE constexpr
The type of this value as a string.

Constructors, destructors, conversion operators

Number(int value) constexpr
Creates a number form an int.
Number(Int value) constexpr
Creates a number form a Int.
Number(Float value) constexpr
Creates a number form a Float.
operator bool() const explicit
Convert the value to a bool (always true).

Public functions

auto to_literal() const -> std::string -> auto
Converts the value to it's literal representation.
auto as_float() const -> Float -> auto
Converts the number to a float.
auto try_as_int() const -> Int -> auto
Returns the number as an int if it is a whole number.
auto convert_to_int() const -> Int -> auto
Convert to Int.
auto is_int() const -> bool -> auto
Check if the number is an int.
template<typename Fn>
auto apply_with_number_rules(const Number& rhs, Fn fn) const -> auto
Invoke the given function with either two Ints or two Floats.

Function documentation

auto minilua::Number::as_float() const -> Float

Converts the number to a float.

If it is an int otherwise just returns the number.

auto minilua::Number::try_as_int() const -> Int

Returns the number as an int if it is a whole number.

Exceptions
std::runtime_error

Otherwise throws an exception.

auto minilua::Number::convert_to_int() const -> Int

Convert to Int.

By casting the Float to an Int (if it is not already an Int).

template<typename Fn>
auto minilua::Number::apply_with_number_rules(const Number& rhs, Fn fn) const

Invoke the given function with either two Ints or two Floats.

If one of the numbers (either this or rhs) is a Float the other one will also be converted to a Float if it is not one.