scala-cardinality

scala-cardinality parses Scala source and reports how many values the types in it can hold.

That number is a design smell detector. A type with few inhabitants leaves a program little room to be wrong: Unit has one value, Boolean two, Option[Boolean] three, Long 2^64, and String as many as you can type. Large or unbounded cardinality is where runtime checks and guesswork come from — see the repository README for the longer version of that argument.

Counting a type

Counter.source("enum Color { case Red, Green, Blue }".parse[Source].get)
// TinySize(3)

Counter.`type`(dialects.Scala3("Either[Boolean, Option[Boolean]]").parse[Type].get)
// TinySize(5)

Counter.source("case class Pixel(shade: Byte, on: Boolean)".parse[Source].get)
// FiniteSize(9)   — 2^8 * 2, held as a 9-bit capacity

These are examples, not mdoc fences: this build cannot run mdoc (see the plugin notes). The behaviour they show is pinned by ArticleCardinalitySpec instead, which fails when a count changes.

Where to go next