Scala type variance
Translating Python TypeVar variance to Scala. Use this skill whenever you see T_co or T_contra in Python and need to write equivalent Scala traits or classes.From its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill scala-type-varianceAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.1 KB, 274 tokens by cl100k_base, as published. Nobody here has run it
Scala Type Variance
- Python
TypeVar("T_co", covariant=True)-> Scala[+T](Covariant). - Python
TypeVar("T_contra", contravariant=True)-> Scala[-T](Contravariant). - Python
TypeVar("T")-> Scala[T](Invariant).
Invariance, Covariance, Contravariance rules in Scala
- Covariant types
[+T]can only appear in covariant positions (e.g., return types of methods). If you need to acceptTin a method parameter, use a lower type bound:def method[U >: T](val: U). - Contravariant types
[-T]can only appear in contravariant positions (e.g., arguments of methods). If you need to returnT, use an upper type bound:def method[U <: T](): U. - Mutable collections (like
ArrayBufferormutable.Map) are invariant[T]. - Immutable collections (like
List,Vector) are covariant[+T]. - Scala functions
A => Bare contravariant inAand covariant inB:[-A, +B].
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.