CSC 347 - Concepts of Programming Languages

Scala Pragmatics

Instructor: Stefan Mitsch

Learning Objectives

  • Set up a Scala development environment

Java and Scala

Using Scala

  • For real programs and homeworks, use sbt to run tests
  • File may only contain object and class declarations
    object o { val x = ... }
    class c { ... }
    
  • You can use console to get a REPL within sbt, use :quit to exit the REPL
  • In the sbt REPL, you can use import objects
    import o.*
    // use x
    

Using Scala

  • For tiny examples, type directly into the REPL
    val x = ... 
    // use x
    
  • For larger examples, type in a file and :load into the REPL
    :load x.sc
    // use x
    
  • File contains declarations just as you would type them in the REPL
  • If there are expressions, then the last one is printed out as a value in the REPL
  • Do not put snippet files ending in .scala in the SBT directory; SBT expects object and class declarations and will report a compile error

Homework Assignments

  • Make sure you are in the right directory: run dir (Windows) or ls (Linux, MacOS) and check that the file build.sbt is listed
  • Compile the homework assignments: inside SBT compile or from command line sbt compile
  • Run all unit tests: inside SBT test or from command line sbt test
  • Run the tests of a single homework assignment: inside SBT testOnly fp1tests or from command line sbt "testOnly fp1tests"
  • Run the homework assignment tests whenever a file changes: inside SBT ~testOnly fp1tests
  • Run a single test of a single homework assignment: inside SBT testOnly fp1tests -- -n fp1ex05