6

I am a newbie to scala. Here is a Models.scala I am trying to write. When I run sbt package it is giving error

Models.scala:25: models.Session.Network.type does not take parameters
[error]         network : Network = Network() ,

I don't understand why this error is taking place, I am not passing any parameter in when doing Network(). Can someone please help me

2 Answers 2

2

Here is a smaller code that reproduces your problem :

case class A(b:B = B(3, 5))

case class B(i: Int, j: Int)

object A {

  val B = "whatever"
}

On the first line, we get

too many arguments for method apply: (index: Int)Char in class StringOps    

What happens is that when you define the signature of the case class, you are both defining the signature of the constructor (when you call with new), and of the apply method in the companion object (when you call without new).

When you put default value in argument, (Network() in your code, and B(3, 5) in mine), this code will be compiled both in the context of the constructor and of the apply method of the companion object.

As you have defined a companion object Session, the apply method is automatically added into this object. It happens that Network() in your companion object means Network.apply() on the Network object you have defined there, and it means the string B with value "whatever" in my code.

What is really weird then is that it is possible that the default expression has different meanings, but both correct in the context of the constructor and of the apply method. In this case, you may get different behavior depending on whether you call with or without new.

Here is an example :

case class A(b:B = bb)

case class B(i: Int, j: Int)

object bb extends B(3, 4)

object A {

  val bb = new B(7, 2)
}


object Test extends App {

  println(A())
  println(new A())

}

Running test will print

A(B(7,2))
A(B(3,4))

For your specific problem, there are easy workarounds.

network: Network = models.Network(),

will work, obviously, because it is then clear that you want Network in the package and not in object Session.

network: Network = new Network(),

will work too, because with the new, the compiler will look for a Network type and not a Network value. In companion object session, the Network value is shadowed by the local declaration, but the Network type is not.

IMO, the former (models.Network) is clearer.


PS. I checked the specification and I believe this weird behavior is in line with it. Namely, (5.3.2) an apply method is genarated inside the companion object with the same parameter list as the constructor. That includes the default values, which would then be compiled inside the companion object.

2
  • I don't know whether this is according to spec and intended (because other behaviors would be too complicated to specify or implement) | according to spec, but an oversight | violating the spec. I will check the spec and if it is still unclear, ask on the mailing list Nov 5, 2014 at 23:23
  • I believe now this is in line with the specification. I notified the mailing list nevertheless. Nov 6, 2014 at 0:46
0

It looks like you may have some import overrides going on. Do you have an import Sessions._ someplace in the code? Notice your error refers to Session.Network, which is your implicit BSonDocument class. You're probably trying to construct the plain case class.

Try using Network explicitly: network: models.Network = models.Network()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.