agentsclimarketplace

Python scala generics variance

Skill cxcscmu/SkillLearnBench/skills/b4-skill-creator-claude-opus-4-6/python-scala-translation/python-scala-generics-variance

Translating Python generics, variance annotations, protocols, and type variables to Scala type parameters, variance annotations, and traits. Use when converting Python Generic[T], TypeVar with covariant/contravariant, Protocol classes, or higher-kinded type simulations to Scala.From its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill python-scala-generics-variance

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

SKILL.md

2.2 KB, 477 tokens by cl100k_base, as published. Nobody here has run it

Python Generics & Variance to Scala

Python Generic[T] → Scala trait/class with type parameter

Python:

T = TypeVar("T")
class BaseTokenizer(ABC, Generic[T]):
    @abstractmethod
    def tokenize(self, value: T) -> Token: ...

Scala:

trait BaseTokenizer[T] {
  def tokenize(value: T): Token
  def tokenizeBatch(values: Iterable[T]): Iterator[Token] =
    values.iterator.map(tokenize)
}

Key: Python's ABC + Generic[T] becomes a Scala trait[T]. Abstract methods need no abstract keyword in traits — just leave them unimplemented.

Variance Annotations

Python:

T_co = TypeVar("T_co", covariant=True)
T_contra = TypeVar("T_contra", contravariant=True)

Scala:

class TokenContainer[+A](items: Seq[A])     // covariant
class TokenSink[-A]                          // contravariant
class BivariantHandler[A](default: A)        // invariant

Python Protocol → Scala trait (structural typing)

Python protocols are structural types. In Scala, use regular traits:

trait Tokenizable {
  def toToken: String
}

Bounded TypeVars → Scala type bounds or overloading

Python TypeVar("NumericT", int, float, Decimal) constrains to specific types. In Scala, use overloaded methods, union-style sealed traits, or context bounds depending on context.

For TypeVar("StrOrBytes", str, bytes):

  • If runtime dispatch is needed, use pattern matching on Any or overloaded methods

Higher-Kinded Type Simulation → Scala class with type param

Python's TokenFunctor simulation translates naturally:

class TokenFunctor[A](private val value: A) {
  def map[B](f: A => B): TokenFunctor[B] = new TokenFunctor(f(value))
  def flatMap[B](f: A => TokenFunctor[B]): TokenFunctor[B] = f(value)
  def get: A = value
}

Note: getOrElse with null-checking becomes Option-based in idiomatic Scala.

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.