refactor(generic): allow int64_t -> double conversion in to_double (#549)

This commit is contained in:
Noah
2025-11-23 17:36:14 +01:00
committed by GitHub
parent 1bda2d005c
commit 331a9794b1
+11 -1
View File
@@ -126,13 +126,23 @@ class RFL_API Generic {
} }
/// Casts the underlying value to a double or returns an rfl::Error, if the /// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double. /// underlying value is not a number or the conversion would result in loss of
/// precision.
Result<double> to_double() const noexcept { Result<double> to_double() const noexcept {
return std::visit( return std::visit(
[](auto _v) -> Result<double> { [](auto _v) -> Result<double> {
using V = std::remove_cvref_t<decltype(_v)>; using V = std::remove_cvref_t<decltype(_v)>;
if constexpr (std::is_same_v<V, double>) { if constexpr (std::is_same_v<V, double>) {
return _v; return _v;
} else if constexpr (std::is_same_v<V, int64_t>) {
auto _d = static_cast<double>(_v);
if (static_cast<int64_t>(_d) == _v) {
return _d;
} else {
return error(
"rfl::Generic: Could not cast the underlying value to a "
"double without loss of precision.");
}
} else { } else {
return error( return error(
"rfl::Generic: Could not cast the underlying value to a " "rfl::Generic: Could not cast the underlying value to a "