[英]Why does Scala query return as unit?
对从多个位置提取的一些数据运行 Squeryl 调用,但由于某种原因它作为一个单元返回。 我如何让它作为 Iterable 返回?
下面是数据的拉取:
/**
* gets a stream for a particular user
*/
def getUserStream(userId:Long) {
User.teamIds(userId).toList.map( (team) =>
Stream.findByTeam(team,0,5).map( (stream) =>
List(stream)
).flatten
).flatten.sortBy(_.id)
}
然后输出数据,结果返回为Unit
:
Stream.getUserStream(userId) match {
case results => {
Ok( generate(results.map( (stream) => Map(
"id" -> stream.id,
"model" -> stream.model,
"time" -> stream.time,
"content" -> stream.content
))
) ).as("application/json")
}
case _ => Ok("")
}
我最初的猜测是一个 function 可以作为 None 返回,但它不会只返回一个空列表吗?
在def getUserStream(userId:Long)
方法主体之前,您缺少等号。
def func(x: Int) { x + 1 } // This will return Unit
def func(x: Int) = { x + 1 } // This will return a Int
添加一点可能有用的内容,比如def f(x: Int) {}
相当于说def f(x: Int): Unit = {}
如果您没有声明返回类型(例如def f(x: Int) = {}
),将从您的方法主体推断类型。
保证返回特定类型的一种技术是显式声明它。 当您想要导出具有特定签名的公共接口时,您会这样做。 这很重要,因为如果您让类型推断器完成所有工作,它可能会公开一个比您想要的更通用的抽象。
def f(x: Int): List[User] = {} // This will not compile.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.