E043: Unreducible Application — 줄일 수 없는 적용이에요
E043: Unreducible Application — 줄일 수 없는 적용이에요
추상 타입 생성자(abstract type constructor, 고차 종류 타입) 에 와일드카드 타입 인자를 적용했을 때 이 에러가 나와요.
본문
이런 적용은 Scala 3에서 지원하지 않는 실존 타입(existential type) 과 동등해요. 추상 타입 생성자에 와일드카드 인자를 적용하면 어떤 구체 타입이 결과로 나올지 컴파일러가 정할 수 없기 때문에, 그렇게 쓸 수 없어요.
예제 (Example)
trait Container[F[_]]:
def create: F[?]
F는 고차 종류 타입인데, 여기에 와일드카드 ?를 적용하고 있어요.
오류 메시지 (Error)
-- [E043] Type Error: example.scala:2:14 ---------------------------------------
2 | def create: F[?]
| ^^^^
| unreducible application of higher-kinded type F to wildcard arguments
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| An abstract type constructor cannot be applied to wildcard arguments.
| Such applications are equivalent to existential types, which are not
| supported in Scala 3.
-----------------------------------------------------------------------------
해결 방법 (Solution)
구체적인 타입 인자를 사용하거나, 타입 멤버(type member)를 사용하거나, 구현에서 타입을 구체적으로 만들면 돼요.
// Use a concrete type argument
trait Container[F[_]]:
def create[A]: F[A]
// Or use a type member
trait Container[F[_]]:
type Element
def create: F[Element]
// Or make the type concrete in implementations
trait Container[F[_]]:
def create: F[Int]