【发布时间】:2021-03-19 23:11:15
【问题描述】:
我使用 ZIO 的第一步,我正在尝试将此 readfile 函数转换为兼容的 ZIO 版本。
下面的sn-p可以编译,但是我没有关闭ZIO版本的源码。我该怎么做?
def run(args: List[String]) =
myAppLogic.exitCode
val myAppLogic =
for {
_ <- readFileZio("C:\\path\\to\file.csv")
_ <- putStrLn("Hello! What is your name?")
name <- getStrLn
_ <- putStrLn(s"Hello, ${name}, welcome to ZIO!")
} yield ()
def readfile(file: String): String = {
val source = scala.io.Source.fromFile(file)
try source.getLines.mkString finally source.close()
}
def readFileZio(file: String): zio.Task[String] = {
val source = scala.io.Source.fromFile(file)
ZIO.fromTry[String]{
Try{source.getLines.mkString}
}
}
【问题讨论】: