-
Notifications
You must be signed in to change notification settings - Fork 1
Operator Overloading
justinmann edited this page Dec 24, 2017
·
3 revisions
complex(
real : 'f64
imag : 'f64
add(num : 'complex) {
complex(real : real + num.real, imag : imag + num.image)
}
subtract(num : 'complex) {
complex(real : real + num.real, imag : imag + num.image)
}
multiply(num : 'complex) {
complex(real : real * num.real + imag * num.image, imag : imag * num.real + real * num.imag)
}
multiplyF64(x : 'f64) {
complex(real : real * x, imag : imag * x)
}
divide(num : 'complex) {
/* However you divide complex numbers */
}
modulus(num : 'complex) {
/* Do whatever, modulus makes no sense for complex numbers */
}
increment(num : 'complex) {
complex(real : real + 1.0, imag : imag)
}
decrement(num : 'complex) {
complex(real : real - 1.0, imag : imag)
}
getDistance() {
sqrt(real * real + imag * imag)
}
setDistance(distance : 'f64) {
factor : distance / getDistance()
complex(real : real * factor, imag : imag * factor)
}
)
a : complex(1.0, 2.0)
b : complex(3.0, 4.0)
a + b
a - b
a * b
a / b
a % b
a++
a--
a.distance
a.distance = 1.0
a * 3.0
// This will find a method by taking 'multiply' concatenated with the type or 'multiplyf32'
// This also works for interfaces & templates but it creates non-standard function names
// which required escaping like this `multiply#foo`() or `multiplyArray!i32`()
// This is compatible with helper functions