An opaque type separates a useful abstraction from its representation. Inside its defining scope, it behaves like the underlying type; outside that scope, callers see an unrelated type. This preserves the efficiency of an alias without exposing its implementation. section 6.5 section 6.8
Modeling state actions
State[S, A] can represent a state computation as S => (A, S). Its defining scope can invoke the underlying function directly, while callers use run to execute it and apply to construct it. Unlike a plain alias, State cannot be confused with an ordinary function; unlike a case-class wrapper, it adds no runtime wrapping cost. section 6.5 section 6.8
opaque type State[S, +A] = S => (A, S)
object State:
extension [S, A](underlying: State[S, A])
def run(s: S): (A, S) = underlying(s)
def apply[S, A](f: S => (A, S)): State[S, A] = f
Inspect the compact State definition, including its opaque representation, run extension method, and apply constructor. section 6.5
The pattern trades a small amount of conversion boilerplate for two benefits: callers receive a genuinely distinct type, and the implementation retains the performance characteristics of the underlying function. section 6.5



