E007: 타입 불일치

E007: 타입 불일치 (Type Mismatch)

이 에러는 표현식의 타입이 그 문맥에서 기대되는 타입과 다를 때 발생해요. 흔히 이런 경우에 일어나요.

출처: Scala 3 Reference

본문

Example

val x: String = 42

Error

-- [E007] Type Mismatch Error: example.scala:1:16 ------------------------------
1 |val x: String = 42
  |                ^^
  |                Found:    (42 : Int)
  |                Required: String
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |
  | Tree:
  |
  | 42
  |
  | I tried to show that
  |   (42 : Int)
  | conforms to
  |   String
  | but none of the attempts shown below succeeded:
  |
  |   ==> (42 : Int)  <:  String
  |     ==> Int  <:  String  = false
  |
  | The tests were made under the empty constraint
   -----------------------------------------------------------------------------

Solution

// Convert the value to the expected type
val x: String = 42.toString
// Or change the type annotation to match the value
val x: Int = 42
// Or use a value of the correct type
val x: String = "42"

더 알아보기

  • 타입 시스템과 서브타입(subtyping)에 대한 자세한 내용은 "Type System" 관련 섹션을 참고하세요.