- Concepts of Programming Languages

Scala Pragmatics

Instructor:

Learning Objectives

  • Set up a Scala development environment

Java and Scala

  • Download and unzip the Scala homework workspace from Assignment 1 on D2L
  • Follow the instructions in README.html
  • If you have Java installed, check Scala JDK Compatibility
  • Install Java LTS (long term support) v21 or
    OpenJDK/AdoptOpenJDK (e.g., via homebrew) v21
  • Install SBT using the windows installer or homebrew
  • Install VSCode and the Metals extension

Homework Assignments: Compile

  • Compile the homework assignments: sbt compile

Make sure you are in the right directory: run dir (Windows) or ls (Linux, MacOS) and check that the file build.sbt is listed

Project loaded, files compiled

  1. [info] set current project to CPL (in build file:[...]/hw1/)
  2. [info] compiling 2 Scala sources and 1 Java source
  3. to [...]/hw1/target/scala-3.3.7/classes ...
  4. [success] Total time: 2 s, completed [...]

No project loaded, no files compiled

  1. [info] set current project to DIR (in build file:[...]/DIR/)
  2. [info] Executing in batch mode. For better performance use sbt's shell
  3. [success] Total time: 0 s, completed [...]

Homework Assignments: Test

  • Run all unit tests: sbt test
  • Auto-run when file changes: sbt "~test"
  • Run a single test: sbt "testOnly gameoflifetests -- -n golex05"

All tests run (task: make them succeed)

  1. [...]
  2. [info] Total number of tests run: 9
  3. [info] Suites: completed 1, aborted 0
  4. [info] Tests: succeeded 0, failed 9, canceled 0, ignored 0, pending 0
  5. [info] *** 9 TESTS FAILED ***
  6. [error] Failed tests:
  7. [error] gameoflifetests
  8. [error] (Test / test) sbt.TestsFailedException: Tests unsuccessful
  9. [error] Total time: 3 s, completed [...]

No tests run: check directory!

  1. [info] set current project to DIR (in build file:[...]/DIR/)
  2. [success] Total time: 0 s, completed [...]

Github Copilot in Homework Assignments

  • See Syllabus: Generative AI in assignments
  • Disable inline suggestions
  • Add keyboard shortcut: ⇧⌘P - Trigger Inline Suggestions (configure shortcut)

Homework Assignment Smart Actions

  • Predefined prompts
    • Create exercise plan /make-exercise-plan
    • Assess style and code quality /assess-exercise
    • Explain compile errors /explain-compile-error
    • Infer types /infer-types
    • Explain code snippets /explain-code

Scala Code Snippets and Scala REPL

  • For tiny examples, type directly into the REPL
    1. val x = ...
    2. // use x
  • For larger examples, type in a file in play and :load into the REPL
    1. :load play/x.sc
    2. // use x
  • File contains declarations, executed one by one just as you would type them
  • The result of the last expression is printed

Do not put snippet files ending in .scala in the SBT directory; SBT expects object and class declarations and will report a compile error

Resources