[英]Scala Option return type
I am newbie in Scala programming world but loving it. 我是Scala编程世界的新手,但喜欢它。 Recently I have started porting my research App into Scala and one of thing I am still struggling is the return keyword.
最近我开始将我的研究应用程序移植到Scala中,而我仍在努力的一件事就是返回关键字。 For example in below code
例如,在下面的代码中
def readDocument(dbobj:MongoDBObject) = Option[ContainerMetaData]
{
for(a <- dbobj.getAs[String]("classname");
b <- dbobj.getAs[Long]("id");
c <- dbobj.getAs[Long]("version");
d <- dbobj.getAs[String]("description");
e <- dbobj.getAs[String]("name");
f <- dbobj.getAs[String]("tag");
g <- dbobj.getAs[Int]("containertype");
h <- dbobj.getAs[Date]("createddate")
)
{
val ctype = ContainerType(g)
val jodadt = new DateTime(h)
val data = new ContainerMetaData(a,b,c,d,e,f,ctype,jodadt)
Some(data)
}
None
}
In above code I get the error message: 在上面的代码中我收到错误消息:
type mismatch; found : None.type required: om.domain.ContainerMetaData
So if I remove the explicit return type the code works but then without explicit return keyword I am not able to terminate my code at Some(data)
. 因此,如果我删除显式返回类型代码工作,但没有显式返回关键字我无法终止我的代码在
Some(data)
。
def readDocument(dbobj:MongoDBObject)=
{
for(a <- dbobj.getAs[String]("classname");
b <- dbobj.getAs[Long]("id");
c <- dbobj.getAs[Long]("version");
d <- dbobj.getAs[String]("description");
e <- dbobj.getAs[String]("name");
f <- dbobj.getAs[String]("tag");
g <- dbobj.getAs[Int]("containertype");
h <- dbobj.getAs[Date]("createddate")
)
{
val ctype = ContainerType(g)
val jodadt = new DateTime(h)
val data = new ContainerMetaData(a,b,c,d,e,f,ctype,jodadt)
Some(data)
}
None
}
And if add a return keyword then compiler complains 如果添加一个return关键字,那么编译器会抱怨
method `readDocument` has return statement; needs result tye
Few more additional info, this is the trait I am extending 更多的额外信息,这是我扩展的特点
trait MongoDAOSerializer[T] {
def createDocument(content:T) : DBObject
def readDocument(db:MongoDBObject) : Option[T]
}
The problem is, that you are missing the yield
keyword in the for-comprehension. 问题是,你在for-comprehension中缺少
yield
关键字。 And also the None
at the end is unnecessary, as the for-comprehension will yield None
, if one of the values is missing and also the explicit creation of a Some
in the comprehension is not needed, as it will create an Option
anyway. 并且最后的
None
是不必要的,因为for-comprehension将产生None
,如果缺少其中一个值并且还不需要在理解中显式创建Some
,因为它将创建一个Option
。 Your code hase to look like this (not tested) 您的代码看起来像这样(未经测试)
def readDocument(dbobj: MongoDBObject): Option[ContainerMetaData] = {
for {
a <- dbobj.getAs[String]("classname")
b <- dbobj.getAs[Long]("id")
c <- dbobj.getAs[Long]("version")
d <- dbobj.getAs[String]("description")
e <- dbobj.getAs[String]("name")
f <- dbobj.getAs[String]("tag")
g <- dbobj.getAs[Int]("containertype")
h <- dbobj.getAs[Date]("createddate")
} yield {
val ctype = ContainerType(g)
val jodadt = new DateTime(h)
new ContainerMetaData(a,b,c,d,e,f,ctype,jodadt)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.