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.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- python-scala-generics-variance
- description
- 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.
03 SKILL.md 正文
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
Anyor 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.
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.