我在Scala中遇到类型错误

时间:2018-04-20 15:55:05

标签: scala-collections

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)

1 个答案:

答案 0 :(得分:0)

您的问题是您没有为数据构造函数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))