当前位置: 动力学知识库 > 问答 > 编程问答 >

Scala Option return type

问题描述:

I am newbie in Scala programming world but loving it. Recently I have started porting my research App into Scala and one of thing I am still struggling is the return keyword. 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).

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

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. 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. 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)
  }
} 
分享给朋友:
您可能感兴趣的文章:
随机阅读: