1

I can't understand why this code is wrong, the error message is:

error: '}' expected but '.' found.

on line

this.tX = cX

code:

class Quaternion {  
  private var tX: Float = 0
  private var tY: Float = 0
  private var tZ: Float = 0
  private var tW: Float = 1

  def this(cX: Float, cY: Float, cZ: Float, cW: Float) {
    this.tX = cX
    this.tY = cY
    this.tZ = cZ
    this.tW = cW
  }

  ...

Please help me fix my probably obvious mistake.

2
  • 1
    Constructor overloading in Scala works a lot different than in Java. There is plenty of resources about this. Just google it and you'll have more than enough. Commented Sep 3, 2013 at 13:06
  • I googled. It took me here. Commented Apr 24, 2015 at 14:59

3 Answers 3

10

Reference like always answers it (§5.3.1 and example 5.3.3):

To prevent infinite cycles of constructor invocations, there is the restriction that every self constructor invocation must refer to a constructor definition which precedes it (i.e. it must refer to either a preceding auxiliary constructor or the primary constructor of the class).

Which in your case change it to:

def this(cX: Float, cY: Float, cZ: Float, cW: Float) {
      this()
      this.tX = cX
      this.tY = cY
      this.tZ = cZ
      this.tW = cW
    }
Sign up to request clarification or add additional context in comments.

Comments

6

I think you should look at making this a case class, and immutable. That would look something like this (nb. generally I prefer doubles to floats):

case class Quaternion(tX: Double = 0.0, tY: Double = 0.0, tZ: Double = 0.0, tW: Double = 1.0)

Instances can then be created as per the following examples:

scala> val qDefault = Quaternion()
qDefault: Quaternion = Quaternion(0.0,0.0,0.0,1.0)

scala> val q1234 = Quaternion(1,2,3,4)
q1234: Quaternion = Quaternion(1.0,2.0,3.0,4.0)

scala> val q0101 = Quaternion(tY = 1.0)
q0101: Quaternion = Quaternion(0.0,1.0,0.0,1.0)

Comments

5

I would strongly recommend to use immutable case class with companion object, constructor overload or default values:

case class Quaternion(cX: Float, cY: Float, cZ: Float, cW: Float)

object Quaternion {
   def apply() = new Quaternion(0,0,0,1)
}

scala> Quaternion()
res0: Quaternion = Quaternion(0.0,0.0,0.0,1.0)

or

case class Quaternion(cX: Float, cY: Float, cZ: Float, cW: Float) {
   def this() = this(0,0,0,1)
}

scala> new Quaternion()
res2: Quaternion = Quaternion(0.0,0.0,0.0,1.0)

or

case class Quaternion(cX: Float = 0, cY: Float = 0, cZ: Float = 0, cW: Float = 1)

scala> Quaternion()
res0: Quaternion = Quaternion(0.0,0.0,0.0,1.0)

If you need to change var, you can always use copy method.

1 Comment

Voted because I think companion object technique is more general than chosen answer. Somehow related, I was looking for this in order to create a case class for a Json Writes. It was useful, but I couldn't use Json.writes because of a known limitation related to apply and Json: github.com/playframework/playframework/issues/2031 . This could be workarounded by not overloading apply: gist.github.com/stephanos/4717443

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.