Skip to content

Multiple overloads of a single operator. #2961

Closed
@brendanzab

Description

@brendanzab

Are multiple overloads of a single operator going to be supported in the future?

To illustrate what I mean here's a simple matrix implementation:

impl mat4_ops for mat4 {

    // ...

    fn *(f: float) -> mat4 { ... }

    fn *(v: vec4) -> vec4 { ... }

    fn *(m: mat4) -> mat4 { ... }

    // ...

}

Here's a simple example that you can compile right away:

import io::println;

type vec3 = { x: float, y: float, z: float };

impl vec3_ops for vec3 {

    // Scales the vector
    fn *(f: float) -> vec3 {
        { x: self.x * f, y: self.y * f,  z: self.z * f }
    }

    // dot product
    fn *(v: vec3) -> float {
        (self.x * v.x) + (self.y * v.y) + (self.z * v.z)
    }

}

fn main() {

    let v1: vec3 = { x: 2.3, y: 8.4, z: 7.9  };
    let v2: vec3 = { x: 4.0, y: 6.7, z: 10.5 };

    let v3 = v1 * 2f;
    println(#fmt("%.2f, %.2f, %.2f", v3.x, v3.y, v3.z));

    let dot_product = v1 * v2;
    println(#fmt("%.2f", dot_product));

}

And this is the error I get when I compile using rust version 0.3:

operatorTest.rs:27:31: 27:33 error: mismatched types: expected `float` but found `{x: float,y: float,z: float}` (float vs record)
operatorTest.rs:27         let dot_product = v1 * v2;
                                                  ^~
operatorTest.rs:28:29: 28:40 error: mismatched types: expected `float` but found `vec3` (float vs record)
operatorTest.rs:28         println(#fmt("%.2f", dot_product));
                                                ^~~~~~~~~~~
note: in expansion of #fmt
operatorTest.rs:28:16: 28:42 note: expansion site
error: aborting due to 2 previous errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions