How can an operator be overloaded for different RHS types and return values?

As of Rust 1.0, you can now implement this: use std::ops::Mul; #[derive(Copy, Clone, PartialEq, Debug)] struct Vector3D { x: f32, y: f32, z: f32, } // Multiplication with scalar impl Mul<f32> for Vector3D { type Output = Vector3D; fn mul(self, f: f32) -> Vector3D { Vector3D { x: self.x * f, y: self.y * f, … Read more

Invoke Operator & Operator Overloading in Kotlin

Yes, you can overload invoke. Here’s an example: class Greeter(val greeting: String) { operator fun invoke(target: String) = println(“$greeting $target!”) } val hello = Greeter(“Hello”) hello(“world”) // Prints “Hello world!” In addition to what @holi-java said, overriding invoke is useful for any class where there is a clear action, optionally taking parameters. It’s also great … Read more