E023: 잘못된 타입 인자 개수
E023: 잘못된 타입 인자 개수 (Wrong Number Of Type Args)
이 에러는 타입 생성자(type constructor)에 잘못된 개수의 타입 인자가 적용될 때 발생해요.
본문
각 제네릭 타입은 특정 개수의 타입 파라미터를 가져요. 예를 들어:
타입이 기대하는 개수의 타입 인자를 정확히 제공해야 합니다.
Example
val x: List[Int, String] = List()
Error
-- [E023] Syntax Error: example.scala:1:7 --------------------------------------
1 |val x: List[Int, String] = List()
| ^^^^^^^^^^^^^^^^^
| Too many type arguments for List[A]
| expected: [A]
| actual: [Int, String]
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| You have supplied too many type parameters
|
| For example List takes a single type parameter (List[A])
| If you need to hold more types in a list then you need to combine them
| into another data type that can contain the number of types you need,
| In this example one solution would be to use a Tuple:
|
| val tuple2: (Int, String) = (1, "one")
| val list: List[(Int, String)] = List(tuple2)
-----------------------------------------------------------------------------
Solution
// Use the correct number of type arguments
val x: List[Int] = List()
// For multiple types, use a tuple or a different container
val x: List[(Int, String)] = List()
// Or use a type that takes multiple parameters
val x: Map[Int, String] = Map()
// Or use Either for two alternatives
val x: Either[Int, String] = Right("hello")
더 알아보기
- 제네릭과 타입 파라미터에 대한 내용은 "Generic Classes / Type Parameters" 관련 문서를 참고하세요.