agentsclimarketplace

Run2 python scala dataclass

Skill cxcscmu/SkillLearnBench/skills/b2-self-feedback-gemini-3.1-pro-preview/python-scala-translation/run2_python-scala-dataclass

Translating Python @dataclass and Enum to Scala case class and Enumeration/Sealed Traits safelyFrom its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill run2_python-scala-dataclass

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, 366 tokens by cl100k_base, as published. Nobody here has run it

Dataclass and Enum to Scala Case Class and Sealed Trait

Python uses @dataclass for concise data records and Enum for enumerations. In Scala, we use case class and sealed abstract class / trait with case object.

Python Enum

class TokenType(Enum):
    STRING = "string"
    NUMERIC = "numeric"

Scala Enum Equivalent (Scala 2.13)

While Scala 3 has enum, in Scala 2.13 we implement Algebraic Data Types explicitly using sealed abstract class (or sealed trait). Abstract class allows taking parameters in constructor.

sealed abstract class TokenType(val value: String)
object TokenType {
  case object STRING extends TokenType("string")
  case object NUMERIC extends TokenType("numeric")
}

Python Dataclass

@dataclass(frozen=True)
class Token:
    value: str
    token_type: TokenType
    metadata: dict[str, Any] = field(default_factory=dict)

Scala Case Class

Case classes are immutable and come with an out-of-the-box copy method.

case class Token(
  value: String,
  tokenType: TokenType,
  metadata: Map[String, Any] = Map.empty
) {
  // Using .copy to create a modified clone
  def withMetadata(kwargs: (String, Any)*): Token = {
    this.copy(metadata = this.metadata ++ kwargs)
  }
}

Notes

  • Python's default_factory translates simply to = Map.empty as default parameters in Scala are evaluated dynamically upon invocation.
  • Python dict translates safely to Scala's immutable Map. For mutability, use scala.collection.mutable.Map.

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.