Trait in prelude::ops auto-imported

Into

trait Into<S, T>
Source: compiler/src/builtin_docs/prelude/ops.hy

Conversion from S into T, written impl Into<T> for S.

Call it as a method where the target type is known: let y: T = x.into();. There are no builtin instances; use as for primitive conversions. Both S and T must be types defined in the current module (an instance with a builtin type such as int on either side is rejected as an orphan).

Required methods#

NameDescription
intoConverts value into a T.

Examples#

use tests::assert;
class Celsius {
    pub c: int,
}

class Fahrenheit {
    pub f: int,
}

impl Into<Fahrenheit> for Celsius {
    pub fn into(Celsius x) -> Fahrenheit {
        return new Fahrenheit(x.c * 9 / 5 + 32);
    }
}

fn main() {
    let f: Fahrenheit = new Celsius(100).into();
    assert!(f.f == 212);
}

See also#

  • int
  • Add: The addition operator +.
  • Sub: The subtraction operator -.
  • Mul: The multiplication operator *.
  • Div: The division operator /.
  • Neg: The unary negation operator -x.
  • Rem: The remainder operator %.
  • Pow: The exponentiation operator **.
↑ ↓ navigate ↵ open