[英]Is it possible in Scala to have functions/methods inside a package{}?
I am currently following Miss Wiem Zine's article (specifically on ZIO): Make your program testable我目前正在关注 Wiem Zine 小姐的文章(特别是关于 ZIO):让您的程序可测试
but I couldn't get pass the但我无法通过
package console {
def println(line: String): ZIO[Console, Nothing, Unit] =
ZIO.accessM(_.console println line)
val readLine: ZIO[Console, Nothing, String] =
ZIO.accessM(_.console.readLine)
}
I've read in alvin alexander's page , but it's all about scoping.我读过alvin alexander 的页面,但都是关于范围界定的。
I tried it in intellij, but it complains.我在intellij中尝试过,但它抱怨。
Could it be a会不会是一个
package object console {
...
}
instead of package console {}
?而不是
package console {}
?
Or is that a scala 3 thing?还是那是 scala 3 的东西? Or am I doing something wrong?
还是我做错了什么?
In Scala 2, we have a concept of package objects.在 Scala 2 中,我们有一个 package 对象的概念。 There are examples here: https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html .
这里有一些例子: https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html 。
I think code snippet from "Wiem Zine's article" is missing object
keyword, so you are right it should be: package object console
, since the whole example is on ZIO and Scala 2.x. I think code snippet from "Wiem Zine's article" is missing
object
keyword, so you are right it should be: package object console
, since the whole example is on ZIO and Scala 2.x. There is no ZIO for Scala 3 yet, as far as I know:-)据我所知,Scala 3 还没有 ZIO :-)
@Thilo is also right that Scala 3 allows us to use top-level definitions right in the file without necessity to wrap into a block. @Thilo 也是正确的,Scala 3 允许我们直接在文件中使用顶级定义,而无需包装成块。 Until Scala 3, one can use
package object
to get common package code.在 Scala 3 之前,可以使用
package object
获得通用的 ZEFE90A8E604A7C840E88D03A7D8 代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.