Python to Scala OOP Patterns

SkillDev tools

Guide for translating Python classes, inheritance, ABC, generics and builder patterns to idiomatic Scala.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Python to Scala OOP Patterns skill

What this skill tells your AI

The instructions your AI receives, as published by cxcscmu/skilllearnbench in skills/b1-one-shot-claude-opus-4-6/python-scala-translation/python-scala-oop/SKILL.md and read by ahel’s review.

Abstract Base Class -> Abstract Class or Trait

// Python: class BaseTokenizer(ABC, Generic[T])
// Scala:
abstract class BaseTokenizer[T] {
  def tokenize(value: T): Token
  def tokenizeBatch(values: Iterable[T]): Iterator[Token] =
    values.iterator.map(tokenize)
}

Generic Classes with Variance

// Covariant container (produces T)
class TokenContainer[+T](items: Seq[T]) {
  private val _items: Vector[T] = items.toVector
  def getAll: Vector[T] = _items
}

// Contravariant sink (consumes T)
class TokenSink[-T] { ... }

// Invariant handler
class BivariantHandler[T](private var _value: T) { ... }

Builder Pattern (Fluent Interface)

// Use `this.type` or return the class itself for chaining
class TokenizerBuilder[T] {
  def withNormalizer(f: String => String): TokenizerBuilder[T] = { ... ; this }
  def build(): T => Token = { ... }
}

// Companion object with apply for factory
object TokenizerBuilder {
  def apply[T](): TokenizerBuilder[T] = new TokenizerBuilder[T]
}

Mutable State

// Python: @dataclass with mutable fields
// Scala: use var or mutable collections explicitly
import scala.collection.mutable

class MutableTokenBatch {
  private val _tokens = mutable.ListBuffer.empty[Token]
  private var _processed = false

  def add(token: Token): Unit =
    if (_processed) throw new RuntimeException("Batch already processed")
    else _tokens += token
}

Signals

GitHub stars
83
Forks
5
Last commit
Jul 2026
Advanced
Catalog kind
skill
Gateway key
python-scala-oop
Source
github.com/cxcscmu/skilllearnbench