두 개의 사례 클래스와 일치하는 비교 함수를 원하지만 약간 자세한 정보가 필요합니다.스칼라에서 일치하는 사례 클래스
잎은 항상 목록에서 정렬 된 순서입니다.
abstract class CodeTree
case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
case class Leaf(char: Char, weight: Int) extends CodeTree
def sortCodeTreeFun(x: CodeTree, y: CodeTree) = {
(x, y) match {
case (x1: Leaf, y1: Leaf) => true
case (x1: Fork, y1: Leaf) => x1.weight < y1.weight
case (x1: Leaf, y1: Fork) => x1.weight < y1.weight
case (x1: Fork, y1: Fork) => x1.weight < y1.weight
}
}
내가 할 CodeTree 생성자를 수정하려고 : 내가 직접 x와 y를 비교할 수 있도록
abstract class CodeTree(weight: Int)
하지만 컴파일러는 말한다 : 생성자 CodeTree에 대한
"충분한 인수 : (무게 : Int) patmat.Huffman.CodeTree "
다른 방법이 있습니까? sortCodeTreeFun 메서드를 줄이십니까?
def sortCodeTreeFun(x: CodeTree, y: CodeTree) = {
(x, y) match {
case (_: Leaf, _: Leaf) => true
case (x1: CodeTree, y1: CodeTree) => x1.weight < y1.weight
}
}
을 그리고 오류의 원인은 당신이 매개 변수를 사용 클래스를 확장 할 때를 제공해야한다는 것입니다
abstract class CodeTree {
def weight: Int
}
로 추상 클래스 CodeTree를 정의
이 영광 코드 4의 온라인 과정 – smk
순수한 위반 "스칼라의 기능 프로그래밍 "물론 도체와 스칼라의 FP에서 숙제 사용할 수 있습니다 나는 부정직하게 코드를 향상 시키려고 노력하지 않을 것이다 .... " –
나는 이미 과제를 완료하고 10/10를 얻었다. 스칼라 언어에 대한 나의 이해를 높이기 위해 불명예스러운 것을 보지 마라. – Zotov