adam-r-kowalski/mongoose

Traits

Opened this issue · 0 comments

Traits can be a mechanism to enable generic programming as well as polymorphism.

For example, how will mongoose know what the expression x + y means?

Currently this is assumed to be an operation on two i64 values. However, this operator can actually mean something else if you pass in two f64 values. Rather than hard coding this into the compiler an Add trait can be utilized.

trait Add[Rhs=Self]:
    Output: Type

    def add(self, rhs: Rhs) -> Output: ...

impl Add for i64:
    Output = Self

    def add(self, rhs: Self) -> Self:
        builtins.i64_add(self, rhs)

impl Add for f64:
    Output = Self

    def add(self, rhs: Self) -> Self:
        builtins.f64_add(self, rhs)

Having an associated Output type as well as a custom Rhs type will allow addition between different types.

impl Add for Vector[Scalar]:
    Output = Self

    def add(self, rhs: Scalar) -> Self:
        ...

Now you would be able to [1, 2, 3] + 4