E003: 폐기된 With 연산자

E003: 폐기된 With 연산자 (Deprecated With Operator)

이 경고는 복합 타입(compound type)을 만들기 위해 타입 연산자로 with를 사용할 때 발생해요. Scala 3에서는 with 대신 &를 쓰는 교집합 타입(intersection type)이 도입되면서 with가 폐기(deprecated)됐거든요.

출처: Scala 3 Reference

본문

Dotty는 교집합 타입, 즉 & 타입을 도입했어요. 이것이 with 키워드의 사용을 대체해요. 교집합 타입과 with 사용 사이에는 의미론에서 몇 가지 차이가 있어요.

Example

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

Warning

-- [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

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

type ReadWrite = Readable & Writable

class File extends Readable, Writable

더 알아보기

  • 교집합 타입(&)에 대한 자세한 내용은 "New Types - Intersection Types" 섹션을 참고하세요.
  • -rewrite -source 3.4-migration 옵션으로 이 코드를 자동으로 다시 쓸 수 있어요.