Function artichoke_backend::extn::core::numeric::coerce[][src]

pub fn coerce(
    interp: &mut Artichoke,
    x: Value,
    y: Value
) -> Result<Coercion, Error>
Expand description

If y is the same type as x, returns an array [y, x]. Otherwise, returns an array with both y and x represented as Float objects.

This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.

See Numeric#coerce.

Coercion enum

Artichoke represents the [y, x] tuple Array as the Coercion enum, which orders its values Coercion::Integer(x, y).


let x = interp.convert(1_i64);
let y = interp.convert_mut(2.5_f64);
assert_eq!(Coercion::Float(1.0, 2.5), numeric::coerce(&mut interp, x, y)?);
let x = interp.convert_mut(1.2_f64);
let y = interp.convert(3_i64);
assert_eq!(Coercion::Float(1.2, 3.0), numeric::coerce(&mut interp, x, y)?);
let x = interp.convert(1_i64);
let y = interp.convert(2_i64);
assert_eq!(Coercion::Integer(1, 2), numeric::coerce(&mut interp, x, y)?);