2017-04-24 2 views
0

Ich habe eine Int: Future [Try [Option [Int]]] von dem ich diesen Int-Wert brauche. Ich habe einen Code wie folgt:Future [Try [Option [Int]]] Fehler gefunden: models.TenantGlobalBranding [Fehler] erforderlich: scala.concurrent.Future [?]

def fun(number: Future[Try[Option[Int]]]): Future[Result] = { 
    val num: Int = number.flatMap(x => processTry(x)) 
} 

def processTry(x: Try[Option[Int]]): Int = processOption(x.getOrElse(Some(101))) 
def processOption(x: Option[Int]): Int = x.getOrElse(101) 

ich diesen Fehler erforderlich: [?] Scala.concurrent.Future

+1

Was ist 'process',' process2'? Was sind ihre Unterschriften? Bitte poste ein [MCVE] deines Problems. –

Antwort

1

Sie können Muster sehr sauber nutzen passende Antwort und die Fehlerfälle zu verarbeiten :

val result: Future[Try[Option[Int]]] 
result.onComplete { 
    case Success(Success(Some(i))) => // you get your i: Int 
    case Success(Success(None)) => // you get None 
    case _ => // Either the `Future` or the `Try` failed, you could also process both cases separately 
} 
0
  1. Ihre number.flatMap erfordert, dass Lambda Rückkehr Future[Int]. Stattdessen wird processTry als : Int definiert. Sie können flatMap durch map ersetzen.
  2. Sie haben num als Int definiert, während das Ergebnis funFuture[Result] ist. Hast du etwas Code verpasst?
  3. val num = ... hat Typ Unit und es unterscheidet sich von Funktionstyp.

könnte der Code wie

aussehen
def fun(number: Future[Try[Option[Int]]]): Future[Result] = { 
    val num: Future[Int] = number.map(x => processTry(x)) 
    num.map{i => Result(i)} 
} 
Verwandte Themen