【发布时间】:2020-09-26 08:17:52
【问题描述】:
我希望if 块返回Right(List[PracticeQuestionTags]),但我不能这样做。 if/else 返回 Either
//I get java.util.List[Result]
val resultList:java.util.List[Result] = transaction.scan(scan);
if(resultList.isEmpty == false){
val listIterator = resultList.listIterator()
val finalList:List[PracticeQuestionTag] = List()
//this returns Unit. How do I make it return List[PracticeQuestionTags]
val answer = while(listIterator.hasNext){
val result = listIterator.next()
val convertedResult:PracticeQuestionTag = rowToModel(result) //rowToModel takes Result and converts it into PracticeQuestionTag
finalList ++ List(convertedResult) //Add to List. I assumed that the while will return List[PracticeQuestionTag] because it is the last statement of the block but the while returns Unit
}
Right(answer) //answer is Unit, The block is returning Right[Nothing,Unit] :(
} else {Left(Error)}
【问题讨论】:
-
您返回的是
Right和Left,它们是Either的实例。这段代码有很多问题,你可能只想用asScaladocs.scala-lang.org/overviews/collections-2.13/… -
抱歉,已更正,
if应该返回Right,else应该返回Left