E188: VarArgs Param Cannot Be Given — varargs 파라미터는 given이 될 수 없음
E188: VarArgs Param Cannot Be Given — varargs 파라미터는 given이 될 수 없음
가변 인자(반복 파라미터, varargs)가 using 또는 implicit 절에 선언됐을 때 발생하는 에러예요.
본문
반복 파라미터는 given/implicit 파라미터로 사용할 수 없어요. 인자를 0개 제공하는 것만으로도 항상 채워질 수 있기 때문에, 암시적 인자로서의 의미가 없어져요.
좀 더 자세한 설명 (Longer explanation)
반복 파라미터 타입을 가진 given을 정의하는 건 불가능해요. 이런 가상의 given 파라미터는 0개의 인자를 제공하는 것만으로도 항상 채워질 수 있기 때문에, given 인자로서의 의미가 사라져요.
예시 (Example)
def example(using xs: Int*): Seq[Int] = xs
에러 (Error)
-- [E188] Syntax Error: example.scala:1:22 -------------------------------------
1 |def example(using xs: Int*): Seq[Int] = xs
| ^^^^
| repeated parameters are not allowed in a using clause
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| It is not possible to define a given with a repeated parameter type. This hypothetical given parameter could always be satisfied by providing 0 arguments, which defeats the purpose of a given argument.
-----------------------------------------------------------------------------
해결 방법 (Solution)
varargs는 일반적인 파라미터 목록에서 사용하세요:
def example(xs: Int*): Seq[Int] = xs
또는 given 파라미터에는 시퀀스 타입을 사용하세요:
def example(using xs: Seq[Int]): Seq[Int] = xs
더 알아보기
using 절에는 반복 파라미터를 쓸 수 없어요. 대신 시퀀스 타입을 쓰거나 일반 파라미터 목록으로 옮기면 돼요.