Packages and Imports
Local Import
Import inside a method, close to where it is used.
local-import
An import can sit inside a method so the name is only visible there. This keeps a helper like `sqrt` scoped to the code that needs it.
Local Import
LocalImport.scala
Replay: real traced execution (multi-file project)
object Main {
def main(args: Array[String]): Unit = {
val n = 16
import scala.math.sqrt
val root = sqrt(n).toInt
println("n=" + n)
println("root=" + root)
}
}
object Main {
def main(args: Array[String]): Unit = {
val n = 9
import scala.math.sqrt
val root = sqrt(n).toInt
println("n=" + n)
println("root=" + root)
}
}
object Main {
def main(args: Array[String]): Unit = {
val n = 25
import scala.math.sqrt
val root = sqrt(n).toInt
println("n=" + n)
println("root=" + root)
}
}
n ← 16, root ← 4
1object Main {2 def main(args: Array[String]): Unit = {3 val n→ 16 = 16 //@n=9, 254 import scala.math.sqrt5 val root→ 4 = sqrt(n)4.0.toInt67 println("n=" + n16)8 println("root=" + root4)9 }10}outputn=16 root=4
n ← 9, root ← 3
1object Main {2 def main(args: Array[String]): Unit = {3 val n→ 9 = 94 import scala.math.sqrt5 val root→ 3 = sqrt(n)3.0.toInt67 println("n=" + n9)8 println("root=" + root3)9 }10}outputn=9 root=3
n ← 25, root ← 5
1object Main {2 def main(args: Array[String]): Unit = {3 val n→ 25 = 254 import scala.math.sqrt5 val root→ 5 = sqrt(n)5.0.toInt67 println("n=" + n25)8 println("root=" + root5)9 }10}outputn=25 root=5