Skip to content

Operator overloading without implementing the respective trait #3820

Closed
@Blei

Description

@Blei

Apparently it is enough to write an anonymous implementation with the correct function name and signature to get the compiler to spit out operator overloading:

struct Thing {
    x: int
}

// Look ma, no Mul!
impl Thing/*: Mul<int, Thing>*/ {
    pure fn mul(c: &int) -> Thing {
        Thing {x: self.x * *c}
    }
}

fn main() {
    let u = Thing {x: 2};
    let _v = u.mul(&3); // Works
    let w = u * 3; // Works!!
    io::println(fmt!("%i", w.x));

    /*
    // This doesn't work though.
    let u2 = u as @Mul<int, Thing>;
    let w2 = u2.mul(&4);
    io::println(fmt!("%i", w2.x));
    */
}

This is probably not intended? And it leads to nasty ICEs when the function name fits, but the function signature doesn't (eg. pure fn mul(c: int) -> Thing, no reference).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemI-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions