silk.core.ops.
The interfaces that give user types the arithmetic, bitwise, and logical operators.
When to use#
Implement Add to give a type +, Multiply to give it *, and so on. Add a bound such
as T: Add<T, T> when a generic function applies an operator to its type parameter.
Details#
These names refer to the sealed Intrinsic operator interfaces (OP-009). The expression a + b
means Add.add(a, b) for the conformance of the type of a. The compiler finds a conformance
in the same places as for other interfaces: the interface source or the module that declares
the type.
Operands move into the member by value. Right and Output let one type combine with another
type, as in Vector * f64. Concrete scalar operands always use the built-in operators. The
comparison operators use silk.core.equal.Equal and silk.core.order.Order.
Gotchas#
If two conformances accept the same operand types, the operator is ambiguous and the compiler reports an error. The compiler does not use the expected result type to select a conformance.
Examples#
Give a type the + operator#
import silk.core.ops { Add }
struct Meters {
value: i32,
}
fn addMeters(left: Meters, right: Meters) -> Meters {
return Meters { value: left.value + right.value }
}
impl Add<Meters, Meters> for Meters {
add: Meters.addMeters
}
pub fn main() -> i32 {
let total = Meters { value: 40 } + Meters { value: 2 }
if total.value != 42 {
return 1
}
return 0
}Import as Add with import silk.core.ops { Add }.
Public declarations: 11.
Add#
pub type Add = Intrinsic.AddThe interface behind left + right.
Details#
A conformance impl Add<Right, Output> for T gives the member
add(left: T, right: Right) -> Output. The expression left + right calls it with both
operands by value. Right and Output can differ from T.
Subtract#
pub type Subtract = Intrinsic.SubtractThe interface behind left - right.
Details#
A conformance impl Subtract<Right, Output> for T gives the member
subtract(left: T, right: Right) -> Output. The expression left - right calls it with both
operands by value. Right and Output can differ from T.
Multiply#
pub type Multiply = Intrinsic.MultiplyThe interface behind left * right.
Details#
A conformance impl Multiply<Right, Output> for T gives the member
multiply(left: T, right: Right) -> Output. The expression left * right calls it with both
operands by value. Right and Output can differ from T.
Divide#
pub type Divide = Intrinsic.DivideThe interface behind left / right.
Details#
A conformance impl Divide<Right, Output> for T gives the member
divide(left: T, right: Right) -> Output. The expression left / right calls it with both
operands by value. Right and Output can differ from T.
Remainder#
pub type Remainder = Intrinsic.RemainderThe interface behind left % right.
Details#
A conformance impl Remainder<Right, Output> for T gives the member
remainder(left: T, right: Right) -> Output. The expression left % right calls it with both
operands by value. Right and Output can differ from T.
Negate#
pub type Negate = Intrinsic.NegateThe interface behind -value.
Details#
A conformance impl Negate<Output> for T gives the member negate(value: T) -> Output. The
expression -value calls it with the operand by value.
BitAnd#
pub type BitAnd = Intrinsic.BitAndThe interface behind left & right.
Details#
A conformance impl BitAnd<Right, Output> for T gives the member
bitAnd(left: T, right: Right) -> Output. The expression left & right calls it with both
operands by value. Right and Output can differ from T.
BitOr#
pub type BitOr = Intrinsic.BitOrThe interface behind left | right.
Details#
A conformance impl BitOr<Right, Output> for T gives the member
bitOr(left: T, right: Right) -> Output. The expression left | right calls it with both
operands by value. Right and Output can differ from T.
BitXor#
pub type BitXor = Intrinsic.BitXorThe interface behind left ^ right.
Details#
A conformance impl BitXor<Right, Output> for T gives the member
bitXor(left: T, right: Right) -> Output. The expression left ^ right calls it with both
operands by value. Right and Output can differ from T.
BitNot#
pub type BitNot = Intrinsic.BitNotThe interface behind ~value.
Details#
A conformance impl BitNot<Output> for T gives the member bitNot(value: T) -> Output. The
expression ~value calls it with the operand by value.
Not#
pub type Not = Intrinsic.NotThe interface behind !value.
Details#
A conformance impl Not<Output> for T gives the member not(value: T) -> Output. The
expression !value calls it with the operand by value.
Category: Core.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.