Last updated on

Week 12 Debrief: Futures, and the last step towards complete webapps

Congrats on completing your 12th week of CS-214! Here is a round-up of interesting questions, tips for exercises and labs, and general notes about the course.

Administrivia

Interesting Ed questions

How does ScalaCheck know how to generate values?

ScalaCheck uses the Gen[T] trait to generate instances of T, using the .sample method. For example, Gen.choose(1, 18) returns a Gen[Int] instance whose sample method picks arbitrary elements in the range 1 .. 18.

But how does ScalaCheck know which Gen[T] instance to use to generate values when checking a property? It uses the Arbitrary[T] typeclass! Arbitrary[T] has just one field: def arbitrary: Gen[T], from which ScalaCheck gets the Gen instance.

This lets us define custom generators and connect them to ScalaCheck’s infrastructure. For example:

enum Direction:
  case North, East, South, West

  def rotate: Direction = this match
    case North => East
    case East  => South
    case South => West
    case West  => North

2025-12-08/gen.sc

import org.scalacheck.{Gen, Arbitrary}

given Arbitrary[Direction] = Arbitrary:
  import Direction.*
  Gen.oneOf(North, South, East, West)

2025-12-08/gen.sc

// Sample one direction
summon[Arbitrary[Direction]].arbitrary.sample

2025-12-08/gen.sc

// Test one property
import org.scalacheck.Prop.forAll

forAll { (d: Direction) =>
  d.rotate.rotate.rotate.rotate == d
}.check()

2025-12-08/gen.sc

Unguided lab updates

Our advice for this week

At this point, you should have a second prototype with almost all functionality complete. In the last few days, we recommend: