Back to Scala3

E062: Types And Traits Cannot Be Implicit

docs/_docs/reference/error-codes/E062.md

3.8.41.2 KB
Original Source

E062: Types And Traits Cannot Be Implicit

This error is emitted when the implicit modifier is used on a type alias or trait definition.

The implicit modifier can only be used on values, methods, and classes (for implicit conversions). Types and traits cannot be implicit because they don't represent values that can be passed implicitly.


Example

scala
implicit trait MyTrait

Error

scala
-- [E062] Syntax Error: example.scala:1:15 -------------------------------------
1 |implicit trait MyTrait
  |^^^^^^^^^^^^^^^^^^^^^^
  |implicit modifier cannot be used for types or traits

Solution

scala
// Remove implicit from trait definitions
trait MyTrait

// Use given instances instead
given myInstance: MyTrait = new MyTrait {}
scala
// For implicit conversions, use a given Conversion
trait Target
class Source

given Conversion[Source, Target] = (s: Source) => new Target {}
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>