title | tocTitle | description |
---|---|---|
Scala Import |
Scala Import |
Scala Import |
- Import classes, objects from other packages
import scala.util
var rnd = new util.Random
println(rnd.nextInt())
- Import classes, objects from other packages
import scala.util.Random
var rnd = new Random
println(rnd.nextInt(1000))
- use specific classes/objects
import scala.util.{Random, Success}
var rnd = new Random
println(rnd.nextInt(1000))
- use _ for importing all
import scala.util._
var rnd = new Random
println(rnd.nextInt(1000))
- Import with alias name
import scala.util.{Random => RandomX}
var rnd = new RandomX
println(rnd.nextInt(1000))
- Hide random, import everything
- Random is not imported
import scala.util.{Random => _, _}
//below lines throw error
var rnd = new Random
println(rnd.nextInt(1000))
- import in scope bountry, -- Random can't be accessed outside scope
def generateRandom : Int = {
import scala.util.{Random}
var rnd = new Random
var n = rnd.nextInt()
return n;
}