The answer to your question might be too long, because it is impossible to know how much Scala you actually know. I will try to make my answer as short as possible, but do not hesitate to ask for clarification at any point. I also apologize on behalf of the whole stackoverflow community for making you feel the need to apologize due to an apparent lack of skill before asking a question.
In Scala 2.10 a concept of Try
was introduced. It is very similar to Option
. Option
is a concept of handling null
s. A value of type Option
can take two forms: Some(value)
or None
. When you have an Option
al value you can pattern match on it to see if it is a Some
or a None
and then act accordingly. Pattern matching occurs in many places in Scala and one of them is during the initialization of val
s. Here are few examples:
val x = 10 // pattern 'x' on the LHS matches any value on the RHS so 'x' is initialized with 10
val Some(x) = Some(10) // pattern 'Some(x)' on the LHS matches any value of type 'Some' and binds it's value to x, so 'x' is yet again initialized with 10
Try
is a concept of handling exceptions. A value of type Try
can take two forms: Success(result)
or Failure(throwable)
. When you have a value of type Try
you can pattern match on it to see if it is a Success
or a Failure
.
This is what happens in your code (pattern matching on Success
). In contrast to Option
the two forms of Try
are not in scope by default, which causes the compilation error. This will fix it:
import scala.util.{Try, Success, Failure}