Files
sakurac/core/traits/arithmetic.sakura
T
2026-12-06 13:52:57 -06:00

119 lines
4.8 KiB
Plaintext

#! @file arithmetic.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the traits of arithmetic: one for each operator, asking for the
#! method `+`, `-`, `*`, `/`, `%`, or a prefix `-` calls between values of one
#! type, and `additive` and `multiplicative`, which gather those that add and
#! those that multiply, and which every number has.
module core;
#! A type whose values add to another of the type, giving one more.
#!
#! `a + b` calls `add`, and `a += b` writes what it gives over `a`. A type has
#! `+` by declaring the method, whether or not it names this trait, and may
#! declare others taking other types, as `instant::add(&this, by: &duration)`.
#! The trait is what generic code asks for to add two values of a type. Numbers
#! are addable without declaring anything.
pub trait addable {
#! Adds another value to this one, as `+` does.
#! @param this The value to add to.
#! @param other The value to add.
#! @returns The sum.
def add(&this, other: &self): self;
}
#! A type whose values subtract from another of the type, giving one more.
#!
#! `a - b` calls `sub`, and `a -= b` writes what it gives over `a`. A type has
#! `-` by declaring the method, and may declare others taking or giving other
#! types, as `instant::sub(&this, since: &instant): duration`. The trait is
#! what generic code asks for to subtract two values of a type. Numbers are
#! subtractable without declaring anything.
pub trait subtractable {
#! Subtracts another value from this one, as `-` does.
#! @param this The value to subtract from.
#! @param other The value to subtract.
#! @returns The difference.
def sub(&this, other: &self): self;
}
#! A type whose values multiply by another of the type, giving one more.
#!
#! `a * b` calls `mul`, and `a *= b` writes what it gives over `a`. A type has
#! `*` by declaring the method, and may declare others taking other types, as
#! `vec2::mul(&this, by: &f64)`. The trait is what generic code asks for to
#! multiply two values of a type, as two matrices. Numbers are multipliable
#! without declaring anything.
pub trait multipliable {
#! Multiplies this value by another, as `*` does.
#! @param this The value to multiply.
#! @param other The value to multiply by.
#! @returns The product.
def mul(&this, other: &self): self;
}
#! A type whose values divide by another of the type, giving one more.
#!
#! `a / b` calls `div`, and `a /= b` writes what it gives over `a`. A type has
#! `/` by declaring the method, and may declare others taking other types, as
#! `duration::div(&this, by: &i64)`. The trait is what generic code asks for to
#! divide two values of a type. Numbers are divisible without declaring
#! anything.
pub trait divisible {
#! Divides this value by another, as `/` does.
#! @param this The value to divide.
#! @param other The value to divide by.
#! @returns The quotient.
def div(&this, other: &self): self;
}
#! A type whose values leave a remainder when divided by another of the type.
#!
#! `a % b` calls `rem`, and `a %= b` writes what it gives over `a`. A type has
#! `%` by declaring the method. The trait is what generic code asks for to take
#! the remainder of two values of a type. Numbers are modular without declaring
#! anything.
pub trait modular {
#! The remainder of dividing this value by another, as `%` gives it.
#! @param this The value to divide.
#! @param other The value to divide by.
#! @returns The remainder.
def rem(&this, other: &self): self;
}
#! A type whose values negate, giving another of the type.
#!
#! `-a` calls `negate`. A type has it by declaring the method. The trait is
#! what generic code asks for to negate a value. Numbers are negatable without
#! declaring anything.
pub trait negatable {
#! Negates this value, as `-x` does.
#! @param this The value to negate.
#! @returns The negated value.
def negate(&this): self;
}
#! A type whose values add, subtract, and negate, all as one type: lengths of
#! time, vectors, matrices, amounts of money.
#!
#! It composes `addable`, `subtractable`, and `negatable`, which give `+`, `-`,
#! and `-x`, beside `equatable`. It asks for no multiplication, since two
#! lengths of time have no product. Numbers are additive without declaring
#! anything.
#!
#! Within a generic bounded by it, a constant does not stand for a value, as it
#! does under `numeric`: a length of time is no number.
pub trait additive : equatable, addable, subtractable, negatable {}
#! A type whose values multiply, as one type: matrices, and whatever is
#! numeric.
#!
#! It composes `multipliable`, which gives `*`, beside `equatable`. It asks for
#! no division, since not every matrix divides another. Numbers are
#! multiplicative without declaring anything.
pub trait multiplicative : equatable, multipliable {}