Skip to content

Instantly share code, notes, and snippets.

@gopalakrishnan-subramani
Created September 10, 2018 11:18
Show Gist options
  • Save gopalakrishnan-subramani/cd8d32d6c54c66b92696b22e4dbb5f3d to your computer and use it in GitHub Desktop.
Save gopalakrishnan-subramani/cd8d32d6c54c66b92696b22e4dbb5f3d to your computer and use it in GitHub Desktop.
title tocTitle description
Scala Import
Scala Import
Scala Import

import a package

  • Import classes, objects from other packages
import scala.util

var rnd = new util.Random
println(rnd.nextInt())

import a class/object

  • Import classes, objects from other packages
import scala.util.Random

var rnd = new Random
println(rnd.nextInt(1000))

import all

  • use specific classes/objects
import scala.util.{Random, Success}

var rnd = new Random
println(rnd.nextInt(1000))

import all

  • use _ for importing all
import scala.util._

var rnd = new Random
println(rnd.nextInt(1000))

import a class/object

  • Import with alias name
import scala.util.{Random => RandomX}

var rnd = new RandomX
println(rnd.nextInt(1000))

import Hide

  • 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 scope

  • 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment