Run2 scala pattern matching dispatch
Type-safe dispatch using Scala pattern matching for polymorphic operationsFrom its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill run2_scala-pattern-matching-dispatchAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.7 KB, 397 tokens by cl100k_base, as published. Nobody here has run it
Pattern Matching for Type Dispatch
Basic Type Dispatch
Scala's pattern matching provides superior type-safe dispatch compared to Python's isinstance checks and duck typing.
def tokenize(value: Any): Token = value match {
case s: String => tokenizeString(s)
case i: Int => tokenizeInt(i)
case d: Double => tokenizeDouble(d)
case l: LocalDateTime => tokenizeDateTime(l)
case null => Token("NULL", NULL)
case _ => Token(value.toString, STRING, Map("fallback" -> true))
}
Benefits Over Python
- Compile-time checking: Patterns are checked for exhaustiveness
- Type narrowing: Within each case, the type is narrowed (s is known to be String)
- Guard clauses: Add conditions with
if
value match {
case s: String if s.nonEmpty => processString(s)
case s: String => Token("EMPTY", STRING)
case _ => fallback(value)
}
Instance and Constant Matching
value match {
case null => handleNull()
case true => handleTrue()
case 0 => handleZero()
case _ => handleOther()
}
Collections Matching
values match {
case Seq() => "empty"
case Seq(single) => s"one: $single"
case Seq(first, rest @ _*) => s"first=$first, count=${rest.length}"
case _ => "unknown"
}
Sealed Trait Matching
tokenType match {
case STRING => "string type"
case NUMERIC => "numeric type"
case TEMPORAL => "temporal type"
case STRUCTURED => "structured type"
case BINARY => "binary type"
case NULL => "null type"
}
When matching on sealed traits, the compiler enforces exhaustiveness.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.