new 표현식에는 타입 인자에 와일드카드를 쓸 수 없어요
new 표현식에는 타입 인자에 와일드카드를 쓸 수 없어요 (E084: Wildcard On Type Argument Not Allowed On New)
new 표현식에서 타입 인자에 와일드카드(? 또는 _)를 쓰면 이 에러가 나요. 객체를 새로 만들 때는 정확한 타입을 알아야 해요.
본문
new 표현식에서 타입 인자에 와일드카드(? 또는 _)를 사용하면 이 에러가 발생해요.
새 인스턴스를 만들 때 컴파일러는 객체를 할당하고 초기화하기 위해 정확한 타입을 알아야 해요. 와일드카드 타입은 알 수 없는 타입이라 직접 인스턴스화할 수 없죠.
예시 (Example)
class Team[A]
val team = new Team[?]
에러 (Error)
-- [E084] Syntax Error: example.scala:3:20 -------------------------------------
3 |val team = new Team[?]
| ^
| Type argument must be fully defined
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Wildcard on arguments is not allowed when declaring a new type.
|
| Given the following example:
|
|
| object TyperDemo {
| class Team[A]
| val team = new Team[?]
| }
|
|
| You must complete all the type parameters, for instance:
|
|
| object TyperDemo {
| class Team[A]
| val team = new Team[Int]
| }
|
-----------------------------------------------------------------------------
해결 방법 (Solution)
구체적인 타입 인자를 사용해요.
// Use a concrete type argument
class Team[A]
val team = new Team[Int]
또는 사용 문맥에서 컴파일러가 타입을 추론하게 두는 방법도 있어요.
// Or let the compiler infer the type from usage context
class Team[A](member: A)
val team = new Team("Alice")