E213: Pointless Applied Constructor Type
E213: Pointless Applied Constructor Type
이 경고는 적용된 생성자 타입(applied constructor type)을 사용했는데 그 결과 타입이 기반 클래스 타입과 같아서 아무 효과가 없을 때 발생해요.
본문
적용된 생성자 타입은 생성자 인자를 타입 자체에 포함시켜 더 정밀한 타입을 표현할 수 있게 해 주는 실험적 기능이에요. 하지만 이 기능은 tracked 파라미터의 타입이 더 구체적인 싱글턴 또는 의존 타입으로 정제될 수 있을 때만 유용해요.
tracked 파라미터의 타입이 List[T]나 String 같은 일반적인 타입이라면, 생성자 인자를 적용해도 클래스 자체보다 더 정밀한 타입을 만들 수 없어요.
예시 (Example)
import scala.language.experimental.modularity
class Container[T](tracked val items: List[T])
def example =
val c: Container[Int](List(1,2,3)) = Container[Int](List(1,2,3))
c
에러 (Error)
-- [E213] Type Warning: example.scala:6:9 --------------------------------------
6 | val c: Container[Int](List(1,2,3)) = Container[Int](List(1,2,3))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|Applied constructor type Container[Int](List(1, 2, 3)) has no effect.
|The resulting type of Container[Int](List(1, 2, 3)) is the same as its base type, namely: Container[Int]
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Applied constructor types are used to ascribe specialized types of constructor applications.
| To benefit from this feature, the constructor in question has to have a more specific type than the class itself.
|
| If you want to track a precise type of any of the class parameters, make sure to mark the parameter as `tracked`.
| Otherwise, you can safely remove the argument list from the type.
-----------------------------------------------------------------------------
해결 방법 (Solution)
이득이 없으므로 적용된 생성자 타입 문법을 제거해요.
import scala.language.experimental.modularity
class Container[T](tracked val items: List[T])
def example =
val c: Container[Int] = Container[Int](List(1,2,3))
c
대안으로, 정밀한 타입 추적이 필요하다면 정제 가능한 파라미터 타입(싱글턴 타입 같은 것)을 사용해요.
import scala.language.experimental.modularity
class Box(tracked val value: Int)
def example =
val box: Box(42) = Box(42)
box