Public def or val leaves its type to inference

ID

scala.maintainability.explicit_result_types

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Api Design

Language

Scala

Description

Reports a non-private def or val member declared without a result type, so the type that callers depend on is whatever inference happens to produce.

Rationale

An inferred member type is part of the public API without anyone having chosen it, and it can widen or narrow on any edit to the body — List becoming Seq, a concrete class becoming a structural or refined type — which breaks callers with no visible change at the declaration. The inferred type is often not the one intended either, and readers of the class have to evaluate the body to learn what the member returns. Annotating also cuts compile time, because the compiler stops re-deriving the type for every user of the member.

The following code illustrates the pattern detected by this rule:

class Catalog(rows: List[String]) {
  // FLAGGED: Public def or val leaves its type to inference
  def names = rows.map(_.trim)
}

Remediation

Write the result type on every non-private member: def names: List[String] = …​. If the member is an implementation detail rather than API, mark it private or private[pkg] — then inference is a local matter and the annotation is optional.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved