Avba
Avba

Reputation: 15296

scala for comprehension with if statement results in compilation error that isn't understood

getting an error when trying to apply a statement after an "if" in the for-yield. not clear why, I've seen similar examples elsewhere

value map is not a member of Long
      c <- f3

when trying to compile this code

def f1() : Try[A]
def f2() : Try[B]
def f3() : Long

val result = for {
              a <- f1
              b <- f2
              if b.status == successcode
              c <- f3 // apply once a and b succeeded, returns a Long , unused result , tried without c<- and directly just f3 but similar syntax error
            } yield a 

Upvotes: 0

Views: 521

Answers (1)

Tim
Tim

Reputation: 27421

The <- syntax is used to map over a collection of some sort, but f3 does not return a collection. f3 just returns a value so use = to assign this value to a result:

val result = for {
          a <- f1
          b <- f2
          if b.status == successcode
          c = f3
        } yield a 

Upvotes: 1

Related Questions