E227: Private Shadows Type — private 필드가 상속받은 필드를 가려요

E227: Private Shadows Type — private 필드가 상속받은 필드를 가려요

private 필드가 같은 이름의 상속받은 필드를 가리면 이 경고가 발생해요. 하위 클래스 안에서 그 이름을 참조할 때 더 이상 부모 타입의 멤버를 가리키지 않게 되므로 코드를 이해하기 어려워질 수 있어요.

이 경고는 -Wshadow:private-shadow 컴파일러 플래그로 켭니다.

출처: Scala 3 Reference

본문

Example

class Parent:
  val value = 1

class Child extends Parent:
  private val value = 2

하위 클래스의 private val value가 부모의 value를 가려요.

Warning

-- [E227] Naming Warning: example.scala:5:2 ------------------------------------
5 |  private val value = 2
  |  ^
  |  value value shadows field value inherited from class Parent
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A private field shadows an inherited field with the same name.
  | This can lead to confusion as the inherited field becomes inaccessible.
  | Consider renaming the private field to avoid the shadowing.
   -----------------------------------------------------------------------------

Solution

상속받은 필드를 가리지 않도록 private 필드의 이름을 바꾸세요.

class Parent:
  val value = 1

class Child extends Parent:
  private val localValue = 2