I am just learning Scala. I created a companion object (see code snippet below) where I define an operator, ^
, (to represent complex conjugation). I have to qualify it with the companion objects name inside the associated class. I was under the impression that I should have unqualified access to the methods of the companion. Can someone tell me if I have done something wrong?
class CQ(val r: Q, val i:Q) {
def +(z : CQ): CQ = {
return new CQ(r + z.r, i + z.i)
}
def -(z: CQ): CQ = {
return new CQ(r - z.r, i-z.i)
}
def *(z: CQ): CQ = {
return new CQ(r*z.r - i*z.i, r*z.i + i*z.r)
}
def /(z: CQ): CQ = {
val d = z.r * z.r + z.i * z.i
val n = this * CQ.^(z) // that I needed to qualify "^" is what I don't get
return new CQ(n.r / d, n.i /d)
}
override def toString = r + " + " + i + "i"
}
object CQ {
def ^(z : CQ) : CQ = {
return new CQ(z.r, Q.NEGONE*z.i)
}
val ONE = new CQ(Q.ONE,Q.ZERO)
val ZERO = new CQ(Q.ZERO, Q.ZERO)
val I = new CQ(Q.ZERO, Q.ONE)
val NEGONE = I * I
}
NOTE: there is another class, Q, that is being used here but is not listed.