与默认构造函数参数的隐式转换相关的奇怪 Scala 错误
考虑文件 bug.scala:
package sandbox
object Foo {
implicit def stringToInt(s: String) = 5
}
import Foo._
class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}
它定义了一个隐式转换,导入它,并在三个略有不同的场景中使用它。编译它:
E:\prog\scala\test>scalac bug.scala
bug.scala:9: error: value / is not a member of java.lang.String
class Boo(val a: Int = "foo" / 3) {
^
one error found
隐式转换在用于默认构造函数参数时似乎不起作用,但适用于其他场景。
现在看一下:
package sandbox
object Foo {
implicit def stringToInt(s: String) = 5
}
object dummy
import Foo._
import dummy._
class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}
我们只是添加了一个空对象并将其导入。现在文件编译没有错误!
我在这里看到两种可能性:1)我的头脑混乱。 2) 编译器存在错误。谁能确认是后者吗?
(使用 Scala 2.8.1。)
Consider the file bug.scala:
package sandbox
object Foo {
implicit def stringToInt(s: String) = 5
}
import Foo._
class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}
It defines an implicit conversion, imports it, and uses it in three slightly different scenarios. Compile it:
E:\prog\scala\test>scalac bug.scala
bug.scala:9: error: value / is not a member of java.lang.String
class Boo(val a: Int = "foo" / 3) {
^
one error found
The implicit conversion seems not to be working when used for a default constructor parameter, but works for the other scenarios.
Now watch this:
package sandbox
object Foo {
implicit def stringToInt(s: String) = 5
}
object dummy
import Foo._
import dummy._
class Boo(val a: Int = "foo" / 3) {
val b: Int = "foo" / 3
def c(d: Int = "foo" / 3) = d
}
We've simply added an empty object and imported it. Now the file compiles without errors!
I see two possibilities here: 1) My head is messing with me. 2) There's a bug in the compiler. Can anyone confirm it's the latter?
(Using Scala 2.8.1.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对是一个错误。我已经替你递交了。
https://issues.scala-lang.org/browse/SI-4141
Definitely a bug. I've lodged it on your behalf.
https://issues.scala-lang.org/browse/SI-4141