home search about rss feed twitter ko-fi

Importing Code, Part 1 (Scala 3 Video)

Import Statements

  • Some built-in Scala types and functions are automatically available to you
    • List, Vector, Seq, Map, Set, String, Int, Double, Boolean, println (and more)
  • These work because these three packages are implicitly imported for you:
java.lang.*
scala.*
scala.Predef.*

Everything else is NOT automatically available:

  • mutable collections
  • every third-party library

In this video I show several ways to use Scala import statements.

To use a third-party library

  • Configure how to access/download it in your build tool (such as by adding the correct entry in build.sbt when using the SBT build tool)
    • (This code is said to be “packaged” so you can use it)
  • Then import it into your code with the correct import statement

Example usage

Import the scala.io.Source library like this:

import scala.io.Source

// then use it
Source.fromFile("/etc/passwd")...

Update: All of my new videos are now on
LearnScala.dev