You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These variants perform range checks to determine whether the number fits into the target type's range. In contrast, number-to-number conversions only provide the throwing variant:
I propose that the #to_x? methods should also be available in the numeric types, including the Big types. This would unify the numeric conversion interface between numbers and strings. Additionally, these methods would play very well with #9389 (the methods there only accept String and Char right now, since nothing else in stdlib defines them).
From the discussion on Gitter there are a few ways to approach this:
Make the convert primitive return nil in case of overflow, then implement the regular conversion methods as to_i? || raise ...; they in turn won't be primitives anymore. The block variant follows naturally, but isn't necessary (String itself doesn't have block variants for #to_f32 and #to_f64). This might have far consequences related to codegen.
Allow the convert primitive to either raise an exception or return nil, so that both #to_i and #to_i? are primitives.
Perform a range check, as numeric comparisons work across types:
defto_i8?
to_i8 ifInt8::MIN<=self<=Int8::MAXend
This should work for integer-to-integer conversions. I am not sure what overflow really means for floating-point types.
Catch any exceptions that result from the usual conversion:
defto_i8?
to_i8 rescuenilend
I think this is the least preferable as it should be possible to determine whether a conversion overflows, without necessitating the use of exceptions.
The text was updated successfully, but these errors were encountered:
Every integer conversion method from
String
, e.g.#to_i
, has 3 variants:These variants perform range checks to determine whether the number fits into the target type's range. In contrast, number-to-number conversions only provide the throwing variant:
I propose that the
#to_x?
methods should also be available in the numeric types, including theBig
types. This would unify the numeric conversion interface between numbers and strings. Additionally, these methods would play very well with #9389 (the methods there only acceptString
andChar
right now, since nothing else in stdlib defines them).From the discussion on Gitter there are a few ways to approach this:
convert
primitive returnnil
in case of overflow, then implement the regular conversion methods asto_i? || raise ...
; they in turn won't be primitives anymore. The block variant follows naturally, but isn't necessary (String
itself doesn't have block variants for#to_f32
and#to_f64
). This might have far consequences related to codegen.convert
primitive to either raise an exception or returnnil
, so that both#to_i
and#to_i?
are primitives.The text was updated successfully, but these errors were encountered: