简体   繁体   English

Scala case class.type不接受参数

[英]Scala case class.type does not take parameters

I am a newbie to scala. 我是斯卡拉的新手。 Here is a Models.scala I am trying to write. 是我想写的Models.scala。 When I run sbt package it is giving error 当我运行sbt包时,它给出了错误

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(). 我不明白为什么会发生这个错误,我在做Network()时没有传递任何参数。 Can someone please help me 有人可以帮帮我吗

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). 会发生的情况是,当您定义案例类的签名时,您既要定义构造函数的签名(当您使用new调用时),也要定义随播对象中的apply方法(当您使用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. 当你将默认值放在参数中时,(代码中的Network()和我的B(3,5)),这个代码将在构造函数和伴随对象的apply方法的上下文中编译。

As you have defined a companion object Session, the apply method is automatically added into this object. 在定义了伴随对象Session后,apply方法会自动添加到此对象中。 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. 碰巧伴随对象中的Network()意味着您在那里定义的Network对象上的Network.apply(),它意味着字符串B在我的代码中具有值“whatever”。

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. 真正奇怪的是,默认表达式可能具有不同的含义,但在构造函数和apply方法的上下文中都是正确的。 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而不是对象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. 也会工作,因为使用new,编译器将查找网络类型而不是网络值。 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. IMO,前者(models.Network)更清晰。


PS. 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. 即,(5.3.2)在伴随对象内部创建apply方法,其具有与构造函数相同的参数列表。 That includes the default values, which would then be compiled inside the companion object. 这包括默认值,然后在随播对象内编译。

It looks like you may have some import overrides going on. 看起来您可能正在进行一些导入覆盖。 Do you have an import Sessions._ someplace in the code? 你在代码中的某个地方有一个导入Sessions._吗? Notice your error refers to Session.Network, which is your implicit BSonDocument class. 请注意,您的错误是指Session.Network,它是您隐式的BSonDocument类。 You're probably trying to construct the plain case class. 您可能正在尝试构建普通的案例类。

Try using Network explicitly: network: models.Network = models.Network() 尝试明确使用网络:network:models.Network = models.Network()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM