我在 Scala 中遇到类型错误

I am getting for type error in Scala

def getCandidateFrameByParentId(parentId: Int, pageNo: Int, pageSize: Int): Future[Pagination]= {
    candidateRepo.getCount(parentId).flatMap{
      count =>candidateRepo.getPage(parentId,pageNo,pageSize).map(frames =>
       frames.map{case (List(Candidate(frameId,phrase,id))) =>CandidatePhrases(id.get,phrase)
         Pagination(count,pageNo,pageSize,List(CandidatePhrases))
      })
    }
}

Error=>Type mismatch, expected: List[CandidatePhrases], actual: List[CandidatePhrases.type]

case class Pagination(count:Int,pageNo:Int,pageSize:Int,candidates:List[CandidatePhrases])

case class CandidatePhrases(id:Int,phrase:String)

你的问题是你没有为数据构造函数提供正确的参数CandidatePhrases:

你应该改变

Pagination(count,pageNo,pageSize,List(CandidatePhrases))

Pagination(count,pageNo,pageSize,List(CandidatePhrases(args)))

其中 args 是您要传递给它的参数。

错误是说您正在创建一个列表,其中的元素是数据构造函数,即 CandidatePhrases.type 类型的元素。这是您正在做的事情的一个小例子:

scala> case class A(v: Int)
defined class A

scala> List(A)
res0: List[A.type] = List(A)

scala> List(A(1))
res1: List[A] = List(A(1))