簡體   English   中英

Scala類型不匹配:找到了Future [MyType]必需Future [Option [MyType]]

[英]Scala type mismatch: found Future[MyType] required Future[Option[MyType]]

我從API獲取Future[MyType] 如何將Future[MyType]轉換為Future[Option[MyType]]

def getApiKey(id: String): Future[Option[MyType]] = Future {
  val g: Future[Option[MyType]] = getID(id) // error mismatch
  g
}

def getID(id: String): Future[MyType] = {
  //return Future[MyType]
}

使用map

def getApiKey(id: String): Future[Option[MyType]] = {
  getID(id).map(Option(_))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM