Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

Scala Type System

БесплатноБез исполняемых скриптовНе проверен

Use when scala's advanced type system including generics, variance, type bounds, implicit conversions, type classes, higher-kinded types, path-dependent…

TheBushidoCollectiveот TheBushidoCollective★ 202 звезды на GitHub

Об этом скилле

Scala Type System

Introduction

Scala features one of the most sophisticated type systems among mainstream programming languages, combining object-oriented and functional programming concepts. This advanced type system enables precise modeling of domain concepts, compile-time verification of complex constraints, and highly reusable abstractions.

Key features include parametric polymorphism (generics), variance annotations, type bounds, implicit conversions and parameters, type classes, higher-kinded types, path-dependent types, and abstract type members. These features enable expressive APIs while maintaining type safety.

This skill covers generics and variance, upper and lower type bounds, view and context bounds, implicit conversions, type classes, higher-kinded types, path-dependent types, and practical type-level programming patterns.

Generics and Type Parameters

Type parameters enable writing reusable code that works with multiple types while maintaining type safety.

// Basic generic class
class Box[T](val content: T) {
  def get: T = content
  def map[U](f: T => U): Box[U] = new Box(f(content))
}

val intBox = new Box(42)
val stringBox = new Box("hello")

// Generic methods
def identity[T](x: T): T = x

def swap[A, B](pair: (A, B)): (B, A) = (pair._2, pair._1)

// Multiple type parameters
class Pair[A, B](val first: A, val second: B) {
  def swap: Pair[B, A] = new Pair(second, first)
}

// Generic collections
def first[T](list: List[T]): Option[T] = list.headOption

def last[T](list: List[T]): Option[T] = list.lastOption

// Type parameter constraints with type bounds
class NumberBox[T <: Number](val value: T) {
  def doubleValue: Double = value.doubleValue()
}

// Generic trait
trait Container[T] {
  def add(item: T): Container[T]
  def get: T
  def isEmpty: Boolean
}

class SimpleContainer[T](private var item: Option[T] = None)
  extends Container[T] {
  def add(newItem: T): Container[T] = {
    item = Some(newItem)
    this
  }

  def get: T = item.getOrElse(throw new NoSuchElementException)

  def isEmpty: Boolean = item.isEmpty
}

// Generic companion object
object Container {
  def empty[T]: Container[T] = new SimpleContainer[T]()

  def of[T](item: T): Container[T] = new SimpleContainer[T](Some(item))
}

// Type parameter inference
val box1 = new Box(42)           // Box[Int]
val box2 = new Box("hello")      // Box[String]
val list1 = List(1, 2, 3)        // List[Int]

// Explicit type parameters when needed
val box3 = new Box[Any](42)
val emptyList = List.empty[String]

// Generic functions with multiple constraints
def max[T](a: T, b: T)(implicit ord: Ordering[T]): T =
  if (ord.gt(a, b)) a else b

println(max(5, 10))              // 10
println(max("apple", "banana"))  // banana

// Type aliases for complex generic types
type StringMap[V] = Map[String, V]
type IntPair = (Int, Int)

val userAges: StringMap[Int] = Map("Alice" -> 30, "Bob" -> 25)
val point: IntPair = (10, 20)

Generics enable writing code once and reusing it with multiple types while maintaining compile-time type safety.

Variance Annotations

Variance controls how parameterized types relate to each other based on their type parameters' subtyping relationships.

// Covariance (+T): if A <: B, then Container[A] <: Container[B]
class CovariantBox[+T](val content: T) {
  def get: T = content
  // Can't have T in contravariant position (method parameters)
  // def set(item: T): Unit = ???  // Won't compile
}

class Animal
class Dog extends Animal
class Cat extends Animal

val dogBox: CovariantBox[Dog] = new CovariantBox(new Dog)
val animalBox: CovariantBox[Animal] = dogBox  // Valid due to covariance

// Contravariance (-T): if A <: B, then Container[B] <: Container[A]
trait Printer[-T] {
  def print(item: T): Unit
}

class AnimalPrinter extends Printer[Animal] {
  def print(animal: Animal): Unit = println("Animal")
}

val animalPrinter: Printer[Animal] = new AnimalPrinter
val dogPrinter: Printer[Dog] = animalPrinter  // Valid due to contravariance

// Invariance (T): no subtyping relationship
class InvariantBox[T](private var content: T) {
  def get: T = content
  def set(item: T): Unit = { content = item }
}

// Practical variance example: Function1
trait Function1[-T, +R] {
  def apply(v: T): R
}

val animalToString: Function1[Animal, String] = animal => "Animal"
val dogToAny: Function1[Dog, Any] = animalToString  // Valid

// Collections variance
val dogs: List[Dog] = List(new Dog, new Dog)
val animals: List[Animal] = dogs  // List is covariant

// Mutable collections are invariant
val dogArray: Array[Dog] = Array(new Dog)
// val animalArray: Array[Animal] = dogArray  // Won't compile

// Option is covariant
val someDog: Option[Dog] = Some(new Dog)
val someAnimal: Option[Animal] = someDog  // Valid

// Variance with multiple type parameters
class Function2[-T1, -T2, +R] {
  def apply(v1: T1, v2: T2): R = ???
}

// Variance bounds in definition
class Box[+T](val content: T) {
  // Use lower
Полный SKILL.md на GitHub

Установить Scala Type System в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл Scala Type System?

Use when scala's advanced type system including generics, variance, type bounds, implicit conversions, type classes, higher-kinded types, path-dependent types, and abstract type members for building type-safe, flexible APIs.

Как установить скилл Scala Type System?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл Scala Type System запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить Scala Type System с