agentsclimarketplace

Run2 scala pattern matching dispatch

Skill cxcscmu/SkillLearnBench/skills/b2-self-feedback-claude-haiku-4-5/python-scala-translation/run2_scala-pattern-matching-dispatch

Type-safe dispatch using Scala pattern matching for polymorphic operationsFrom its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill run2_scala-pattern-matching-dispatch

Assembled 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.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.