-1

I am trying to use the code below in scala, using GraphX

val vertexRDD: RDD[(VertexId, String)] = graph.vertices.filter({
        case (id, (str)) => {
            val c: Boolean = scala.util.Try(str.toInt) match {
                case Success(_) => false
                case _ => true
            }
        }
    })

This function is with the official interface def filter(pred: Tuple2[VertexId, VD] => Boolean): VertexRDD[VD]

However it throws a type mismatch error

[error]  found   : Unit
[error]  required: Boolean
[error]             }
[error]             ^

How could it be? I have already specified the return to be Boolean and it is really Boolean, am I right?...

Litchy
  • 355
  • 1
  • 4
  • 18
  • 2
    You have assigned the bool to `c` and an assignment will return `Unit`. To return the bool, either remove the assignment or put `c` in the last row to return it. – Shaido May 08 '18 at 03:20
  • 1
    @Shaido: Why do you give your answer as comment? Now the question shows up as unsolved, until someone repeats your comment as answer. – user unknown May 08 '18 at 03:46
  • 1
    @userunknown I voted to close the question as "simple typographical error", but maybe I will add it as an answer later on if it's still open & unanswered. – Shaido May 08 '18 at 03:51
  • A typographical error? But that's not the case. – user unknown May 08 '18 at 05:34

1 Answers1

2

The reason this fails is that the value of a block is the value of the last expression in the block, but unfortunately the last expression in your block is a declaration which has type Unit. To fix this you can just remove the declaration.

You can also simplify your code by using Try.isSuccess and removing some unnecessary brackets

val vertexRDD: RDD[(VertexId, String)] = graph.vertices.filter{
    case (_, (str)) =>
        scala.util.Try(str.toInt).isSuccess        
}
Tim
  • 26,753
  • 2
  • 16
  • 29