Scala case class.type 不带参数 [英] Scala case class.type does not take parameters

查看:49
本文介绍了Scala case class.type 不带参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手.这里是我正在尝试编写的 Models.scala.当我运行 sbt 包时出现错误

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() ,

我不明白为什么会发生这个错误,我在执行 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

推荐答案

这是一个较小的代码,可以重现您的问题:

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"
}

在第一行,我们得到

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

发生的情况是,当您定义 case 类的签名时,您同时定义了构造函数的签名(当您使用 new 调用时)和伴生对象中的 apply 方法的签名(当您不使用 new 调用时).

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).

当你把默认值放在参数中时,(Network() 在你的代码中,而 B(3, 5) 在我的)中,这段代码将在构造函数的上下文和伴随的 apply 方法的上下文中编译目的.

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.

因为你已经定义了一个伴生对象 Session,apply 方法会自动添加到这个对象中.碰巧你的伴生对象中的 Network() 意味着你在那里定义的 Network 对象上的 Network.apply() ,它意味着我的代码中值为whatever"的字符串 B.

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.

真正奇怪的是,默认表达式可能具有不同的含义,但在构造函数和 apply 方法的上下文中都是正确的.在这种情况下,您可能会得到不同的行为,具体取决于您是否使用 new 调用.

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.

这是一个例子:

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())

}

运行测试将打印

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

<小时>

对于您的具体问题,有一些简单的解决方法.


For your specific problem, there are easy workarounds.

network: Network = models.Network(),

显然会起作用,因为很明显您希望 Network 包含在包中而不是对象 Session 中.

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

network: Network = new Network(),

也可以工作,因为使用 new 时,编译器将查找 Network 类型而不是 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,前者(models.Network)更清晰.

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

附注.我检查了规范,我相信这种奇怪的行为是符合规范的.即,(5.3.2)在伴随对象内部生成了一个应用方法,其参数列表与构造函数相同.这包括默认值,然后将在伴随对象内编译.

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.

这篇关于Scala case class.type 不带参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆