如果将一个变量声明为lazy,则只有在第一次使用该变量时,变量对应的表达式才会起作用。

例如:对于特别耗时的的计算操作特别有用,如打开文件IO,网络IO等。

import scala.io.Source._

lazy val lines = fromFile("C:/Users/18458/Desktop/test.txt").mkString

print(lines)
hello world
scala !!!

相关文章:

  • 2021-08-06
  • 2021-08-18
  • 2022-02-26
  • 2021-11-25
  • 2021-07-04
  • 2021-09-02
  • 2022-12-23
  • 2021-04-27
猜你喜欢
  • 2021-09-11
  • 2021-06-22
  • 2021-07-07
  • 2022-12-23
  • 2021-09-04
  • 2022-01-13
  • 2021-07-17
相关资源
相似解决方案