IT TIP

스칼라의 봉인 된 추상 대 추상 클래스

itqueen 2020. 11. 19. 22:44
반응형

스칼라의 봉인 된 추상 대 추상 클래스


sealed abstractabstractScala 클래스 의 차이점은 무엇입니까 ?


차이점은 봉인 된 클래스의 모든 하위 클래스 (추상 여부에 관계없이)는 봉인 된 클래스와 동일한 파일에 있어야한다는 것입니다.


대답 했듯이 봉인 된 클래스 (추상 여부)의 모든 직접 상속 하위 클래스는 동일한 파일에 있어야합니다. 이것의 실질적인 결과는 패턴 일치가 불완전한 경우 컴파일러가 경고 할 수 있다는 것입니다. 예를 들면 :

sealed abstract class Tree
case class Node(left: Tree, right: Tree) extends Tree
case class Leaf[T](value: T) extends Tree
case object Empty extends Tree

def dps(t: Tree): Unit = t match {
  case Node(left, right) => dps(left); dps(right)
  case Leaf(x) => println("Leaf "+x)
  // case Empty => println("Empty") // Compiler warns here
}

(가) 경우 Tree입니다 sealed마지막 줄을 주석 해제하지 않는 한, 다음 컴파일러는 경고합니다.

참고 URL : https://stackoverflow.com/questions/3032771/scalas-sealed-abstract-vs-abstract-class

반응형