AGENT SKILL

ams-scala-pro

暂无简介

License 许可
MIT
所属仓库
TanNhatCMS/jarvis
最近更新
2026-11-07
Stars
★ 0
Forks
1

03 SKILL.md 正文

Scala Pro

Overview

Scala's strength — a principled blend of functional and object-oriented programming on the JVM — is also its risk: the language lets you write Java-with-sugar, Haskell-on-the-JVM, or anything in between. Professional Scala means picking a coherent style for the codebase (and sticking to it): immutable data with case classes, Option/Either instead of nulls/exceptions for expected cases, for-comprehensions for sequencing, and an effect story the team understands.

The through-line: expressive types, immutable data, explicit effects — and consistency across the codebase.

When to use

  • Writing or reviewing Scala for idiom (Scala 2.13 or 3).
  • Modeling domains with case classes, ADTs, and pattern matching.
  • Choosing effect management (Future, cats-effect, ZIO).
  • Structuring Scala projects (sbt, modules, layering).
  • Debugging implicits/givens, type inference, or performance issues.

Core concepts

  • Case classes + ADTs. case class for immutable data (structural equality, pattern matching, copy for updates); sealed trait hierarchies for variants with exhaustive matching. This pair models most domains better than any framework.
  • Option/Either/Try over nulls and exceptions. Option for absence, Either[E, A] for failures as values, exceptions only for the truly exceptional. For-comprehensions sequence these cleanly — learn to read and write them fluently.
  • Immutability by default. val not var; immutable collections (List, Vector, Map); transformations return new values. var is occasionally pragmatic (local accumulators, perf hotspots) — never shared mutable state.
  • Effects, one story. Future for simple async interop (mind the ExecutionContext); cats-effect IO or ZIO for principled effect management with resource safety (Resource/ZManaged for acquire-release). Don't mix raw Futures and IO in the same codebase without a clear boundary.
  • Implicits (Scala 2) / givens (Scala 3): powerful, dangerous. Use for typeclass instances and DI-ish context passing; avoid implicit conversions (Scala 3 wisely makes them explicit). When a newcomer can't find where a value comes from, you've overused them.
  • Pattern matching as control flow. Destructure in matches, extract with custom extractors sparingly, and let exhaustiveness checking (-Xfatal-warnings + sealed traits) catch missing cases.

Practical workflow

  1. Scaffold: sbt project, Scala 3 (or 2.13 for legacy ecosystems), scalafmt enforced in CI, -Xfatal-warnings with -Wunused to keep the codebase honest.
  2. Model the domain. Sealed traits + case classes; smart constructors (object factories validating input) where invariants matter; no nulls crossing boundaries.
  3. Choose the effect story early. Team decision, documented: plain Futures, cats-effect IO, or ZIO. Consistency beats optimality — a mixed codebase pays both ecosystems' complexity costs.
  4. Structure in layers. Domain (pure, no effects) → services (effectful orchestration) → interpreters/adapters (DB, HTTP, messaging). The domain stays testable without mocks.
  5. Test behaviorally. ScalaTest or munit for unit tests, property-based (ScalaCheck) for invariants, and integration tests with real dependencies (testcontainers) at the adapter layer.
  6. Watch compile times and binary weight. Scala compiles slowly — modularize (sbt subprojects), avoid implicit-heavy megatraits, and profile before blaming the language.

Idiomatic snippets:

// ADT + exhaustive matching
sealed trait OrderStatus
object OrderStatus:
  case object Pending extends OrderStatus
  case object Paid extends OrderStatus
  case object Shipped extends OrderStatus
  case object Cancelled extends OrderStatus

def canCancel(s: OrderStatus): Boolean = s match
  case OrderStatus.Pending | OrderStatus.Paid => true
  case OrderStatus.Shipped | OrderStatus.Cancelled => false

// For-comprehension sequencing Either
def placeOrder(cmd: PlaceCmd): Either[OrderError, Order] =
  for
    cart  <- validateCart(cmd.cart)
    priced <- priceCart(cart)
    order <- persist(priced)
  yield order

Common pitfalls

  • Style schizophrenia. Half the codebase in Java-style mutable OO, half in Haskell-style point-free. Agree on the team's Scala dialect (document it) and review for consistency.
  • Exceptions for expected failures. Throwing for validation errors or "not found" in normal flow. Either/Option for expected cases; exceptions for programmer errors and truly exceptional infrastructure failures.
  • null leaking in. From Java interop or careless code — wrap Java APIs at the boundary (Option(x)), never let null flow through Scala code.
  • Implicit/given soup. Instances and conversions that make code magical and undebuggable. Explicit is better than implicit when the reader can't trace the source.
  • Blocking in Futures. Calling blocking I/O inside the default ExecutionContext starves it. Dedicated (blocking) context for blocking calls, or an effect system that manages shifting.
  • Over-abstracted typelevel gymnastics. Shapeless/HList wizardry and 5-parameter typeclasses for business CRUD. The type system serves the domain — when the types are harder than the problem, simplify.
  • Ignoring compile-time cost. Massive implicit resolution and macro expansion making builds take 20 minutes. Modularize, reduce implicit scope, and treat build time as a team tax.

04 同仓库更多 Skills

TanNhatCMS
TanNhatCMS/jarvis

web-scrape

Turn web pages into LLM-ready markdown via the Firecrawl API (renders JS, anti-bot handling). Use when browser_open/fetch fails on JS-heavy pages, or when you need clean markdown/structured extraction from a site. Requires FIRECRAWL_API_KEY.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

design-taste-frontend

tasteskill v2, the anti-slop frontend skill. Art direction for websites, distilled from a study of 247 curated gallery sites (One Page Love, Awwwards Sites of the Day). Deals researched directions (idea, composition, type, palette, photographs, section blueprints) from a library, enforces a strict no-slop house style with a real-browser inspector, and runs a look-and-fix loop on screenshots. Use this whenever you build, design, redesign, restyle, polish or review a landing page, marketing site, portfolio, product page, one-pager, hero or any public-facing web page, in any stack (HTML/CSS, React, Next.js, Vue, Svelte, Astro, Tailwind) — also when the user says a page looks generic, templated, "AI-made", sloppy, or asks to make it look designed, premium, award-level or less like every other site. Not for dashboards or app screens.

★ 0
TanNhatCMS
TanNhatCMS/jarvis

test-driven-development

Use when implementing any feature or bugfix, before writing implementation code

★ 0
TanNhatCMS
TanNhatCMS/jarvis

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

★ 0
TanNhatCMS
TanNhatCMS/jarvis

requesting-code-review

Use when completing tasks, implementing major features, or before merging to verify work meets requirements

★ 0
TanNhatCMS
TanNhatCMS/jarvis

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

★ 0