Back to Scala3

E003: Deprecated With Operator

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

3.8.41.9 KB
Original Source

E003: Deprecated With Operator

This warning is emitted when using with as a type operator to create compound types. In Scala 3, with has been deprecated in favor of intersection types using &.

Dotty introduces intersection types - & types. These replace the use of the with keyword. There are a few differences in semantics between intersection types and using with.


Example

scala
trait A
trait B
def test(x: A with B): Unit = ()

Warning

scala
-- [E003] Syntax Warning: example.scala:3:14 -----------------------------------
3 |def test(x: A with B): Unit = ()
  |              ^^^^
  |with as a type operator has been deprecated; use & instead
  |This construct can be rewritten automatically under -rewrite -source 3.4-migration.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Dotty introduces intersection types - & types. These replace the
  | use of the with keyword. There are a few differences in
  | semantics between intersection types and using with.
   -----------------------------------------------------------------------------

Solution

scala
// Use intersection type operator & instead of with
trait A
trait B
def test(x: A & B): Unit = ()
scala
// The change also applies to type aliases and class definitions
trait Readable
trait Writable

type ReadWrite = Readable & Writable

class File extends Readable, Writable
<!-- 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>