Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala compilation error

Can't figure out what's wrong with StrangeIntQueue extending Queue, why there is an error "Not enough arguments for constructor Queue: (leading: Int)list.Lister.Queue[Int]. Unspecified value parameter leading". How can I specify it?

  class Queue[+T](
    private val leading: T
  ) {
    def enqueue[U >: T](x: U) =
      new Queue[U](leading: U) // ...
  }

  class StrangeIntQueue(private val leading: Int) extends Queue[Int] {
    override def enqueue(x: Int) = {
      println(math.sqrt(x))
      super.enqueue(x)
    }
  }
like image 508
Vadim Samokhin Avatar asked Feb 27 '12 20:02

Vadim Samokhin


People also ask

How do you fix a compilation error?

If the brackets don't all match up, the result is a compile time error. The fix to this compile error is to add a leading round bracket after the println to make the error go away: int x = 10; System.

What is scalacOptions?

As shown, scalacOptions lets you set Scala compiler options in your SBT project build. I got that example from my earlier very large Scala/SBT build. sbt example. Also, I just saw that tpolecat has put together a nice list of scalaOptions flags at this URL.


2 Answers

extends Queue[Int](leading)

You have to pass on the arguments even if it seems "obvious" what to do.

Note also that since you have declared leading private, you'll actually get two copies: one for StrangeIntQueue and one for Queue. (Otherwise you could have just StrangeIntQueue(leading0: Int) extends Queue[Int](leading0) and use the inherited copy of leading inside.)

like image 156
Rex Kerr Avatar answered Sep 26 '22 05:09

Rex Kerr


The primary constructor of class Queue, which StrangeIntQueue extends, takes a parameter, but you're not passing it anything for the parameter. Try this:

class StrangeIntQueue(leading: Int) extends Queue[Int](leading) {
  // ...
}
like image 43
Jesper Avatar answered Sep 25 '22 05:09

Jesper