Inline and Implicit Analysis¶
This pair of metrics surfaces metaprogramming and API ergonomics signals that are easy to miss in code review.
What it Measures¶
inline(Scala 3):inline def/val/var: definitions with the inline modifier.inlineparameters declared asinline p: T.
usingcount (Scala 3).givens(Scala 3):- All given instances (given definitions).
- Given conversions:
given Conversion[A, B].
@inlinemethods (Scala 2).implicitconversions: heuristic subset of implicit defs that look likeimplicit def f(a: A): B(single non-implicitparameter list, non-Unitresult). These are the ones that most often surprise readers and JIT (Scala 2).
Why it Helps¶
- Visibility into compile-time metaprogramming/optimizations; prevents overuse that may bloat the code or slows down compile.
- Surfaces API ergonomics; too many context requirements can harm discoverability.
Inline & Implicit Usage: Power with Restraint¶
inline can erase tiny abstractions in hot paths, and given/implicits enable elegant APIs. Both increase
compile‑time cleverness. Use them when they clearly help, document intent, and keep the surface area small.
1. Inline for tiny hot-path helpers¶
This removes call overhead and enables constant folding in simple cases. Keep inline bodies tiny and pure; larger bodies hurt compile times and readability.
Note
Measure before/after. Keep inline bodies small and side-effect free.
2. Given instances with clear names and scopes¶
Prefer local/module scopes so the resolution is predictable. Tests can supply alternative givens, and call sites remain
explicit with using when needed.
Warning
Prefer local givens or module-level scopes; avoid surprising global instances.
3. Document the magic¶
Anytime you lean on inline or givens/implicits, write one or two sentences explaining the why. Future maintainers
will thank you, and reviewers can assess whether the cleverness is worth it.