E104: Trait Is Expected

This error is emitted when a class is used in a position where only a trait is allowed.

In Scala, only traits can be mixed into classes using the with keyword. Classes cannot be mixed in this way.


Example

class A
class B

val example = new A with B

Error

-- [E104] Syntax Error: example.scala:4:25 -------------------------------------
4 |val example = new A with B
  |                         ^
  |                         class B is not a trait
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Only traits can be mixed into classes using a with keyword.
  | Consider the following example:
  |
  | class A
  | class B
  |
  | val a = new A with B // will fail with a compile error - class B is not a trait
  |
  | The example mentioned above would fail because B is not a trait.
  | But if you make B a trait it will be compiled without any errors:
  |
  | class A
  | trait B
  |
  | val a = new A with B // compiles normally
   -----------------------------------------------------------------------------

Solution

// Change the class to a trait
class A
trait B

val example = new A with B
// Or use extends instead of with
class A
class B extends A

val example = new B()