Chris@44: Chris@44: module complex; Chris@44: Chris@46: import java.lang: ClassCastException; Chris@46: Chris@45: class Complex(double real, double imag) Chris@44: int getReal() Chris@44: real, Chris@44: int getImag() Chris@44: imag, Chris@44: String toString() Chris@44: "\(real) + \(imag)i", Chris@46: int hashCode() Chris@46: Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(), Chris@46: boolean equals(Object other) Chris@46: try Chris@46: c = other unsafely_as ~Complex; Chris@46: c#getReal() == real and c#getImag() == imag Chris@46: catch ClassCastException: Chris@46: false Chris@46: yrt, Chris@44: end; Chris@44: Chris@44: typedef opaque cplx = ~Complex; Chris@44: Chris@46: real c1 is ~Complex -> number = Chris@46: c1#getReal(); Chris@46: Chris@46: imaginary c1 is ~Complex -> number = Chris@46: c1#getImag(); Chris@46: Chris@46: complex re im is number -> number -> ~Complex = Chris@46: new Complex(re, im); Chris@46: Chris@46: add c1 c2 is ~Complex -> ~Complex -> ~Complex = Chris@46: complex (real c1 + real c2) (imaginary c1 + imaginary c2); Chris@46: Chris@46: scale r c is number -> ~Complex -> ~Complex = Chris@46: complex (r * real c) (r * imaginary c); Chris@44: Chris@44: { Chris@46: real, Chris@46: imaginary, Chris@46: complex, Chris@46: add, Chris@46: scale, Chris@46: i = complex 0 1, Chris@46: one = complex 1 0, Chris@46: zero = complex 0 0, Chris@44: } as { Chris@46: real is cplx -> number, Chris@46: imaginary is cplx -> number, Chris@46: complex is number -> number -> cplx, Chris@46: add is cplx -> cplx -> cplx, Chris@46: scale is number -> cplx -> cplx, Chris@46: i is cplx, Chris@46: one is cplx, Chris@46: zero is cplx, Chris@44: } Chris@44: