agentsclimarketplace

Run1 python to scala translation patterns

Skill cxcscmu/SkillLearnBench/skills/b3-teacher-feedback-claude-sonnet-4-6/python-scala-translation/run1_python-to-scala-translation-patterns

Use when translating Python OOP/functional code to idiomatic Scala 2.13, covering class hierarchies, enumerations, error handling, and naming conventionsFrom its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill run1_python-to-scala-translation-patterns

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

SKILL.md

3.9 KB, ~1.0k tokens by cl100k_base, as published. Nobody here has run it

Python to Scala Translation Patterns

Class and Object Hierarchy

Enumerations

Python Enum → Scala sealed trait + case object:

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

// Scala
sealed trait TokenType
object TokenType {
  case object String  extends TokenType
  case object Numeric extends TokenType
  // companion object provides namespace
}

Abstract Base Classes

Python ABC / abstract methods → Scala abstract class or trait:

// Python
class BaseTokenizer(ABC):
    @abstractmethod
    def tokenize(self, text: str) -> List[Token]: ...

// Scala
abstract class BaseTokenizer {
  def tokenize(text: String): List[Token]
}
// OR as a trait (preferred for mixin composition)
trait BaseTokenizer {
  def tokenize(text: String): List[Token]
}

Data Classes / Named Tuples

Python @dataclass / NamedTuple → Scala case class:

// Python
@dataclass
class Token:
    value: str
    token_type: TokenType
    metadata: dict = field(default_factory=dict)

// Scala
case class Token(
  value: String,
  tokenType: TokenType,
  metadata: Map[String, Any] = Map.empty
)

Naming Conventions

ConceptPythonScala
ClassPascalCasePascalCase
Method/fieldsnake_casecamelCase
ConstantUPPER_SNAKEUpperCamelCase or camelCase val
Packagelowercaselowercase
Type paramTT
// Python: token_type, is_valid, to_token
// Scala: tokenType, isValid, toToken

Optional / None Handling

Python Optional[T] / None → Scala Option[T]:

// Python
def find(text: str) -> Optional[Token]:
    if condition: return Token(...)
    return None

// Scala
def find(text: String): Option[Token] =
  if (condition) Some(Token(...)) else None

Error Handling

Python try/except with custom exceptions → Scala Try, Either, or Option:

import scala.util.{Try, Success, Failure}

// Python
try:
    result = risky()
except ValueError as e:
    return None

// Scala — Try
def risky(): Try[Token] = Try { /* may throw */ }

// Scala — Either for typed errors
def parse(s: String): Either[String, Token] =
  if (valid(s)) Right(Token(s, ...))
  else Left(s"Invalid input: $s")

Collections

PythonScala
listList[T] or Seq[T]
dictMap[K, V]
setSet[T]
tuple(A, B) or case class
List[str]List[String]
Dict[str, Any]Map[String, Any]
// Python: [t for t in tokens if t.value != ""]
// Scala:
tokens.filter(_.value.nonEmpty)

// Python: [f(x) for x in xs]
// Scala:
xs.map(f)

// Python: sum(len(t.value) for t in tokens)
// Scala:
tokens.map(_.value.length).sum

Companion Objects (replacing Python class methods / static methods)

// Python @classmethod or @staticmethod
class Token:
    @staticmethod
    def from_string(s: str) -> Token: ...

// Scala companion object
case class Token(value: String, tokenType: TokenType)
object Token {
  def fromString(s: String): Token = ...
}

Pattern Matching (replaces isinstance / match)

// Python
if isinstance(x, StringTokenizer): ...
elif isinstance(x, NumericTokenizer): ...

// Scala
x match {
  case _: StringTokenizer  => ...
  case _: NumericTokenizer => ...
}

// On sealed traits
tokenType match {
  case TokenType.String  => ...
  case TokenType.Numeric => ...
}

Default Parameters and Named Arguments

// Python
def tokenize(text: str, lower: bool = True) -> List[Token]: ...
tokenize("Hello", lower=False)

// Scala
def tokenize(text: String, lower: Boolean = true): List[Token] = ...
tokenize("Hello", lower = false)

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.