Mohsen Mohsen - 1 year ago 80
Scala Question

Scala: Why do I get a type mismatch error when I use "Option"?

I'm new to Scala. I have a Scala function, and one of its arguments uses "Option":

def generateTimeSnippet(startOfSentence: Double, endOfSentence: Option[Double]): (Double, Option[Double]) = {

When I call this function, I give literal values to the arguments:

val snippets = generateTimeSnippet(startOfSentence = 10d, endOfSentence = 20.5)

But this results in a type mismatch error:
"type mismatch; found : Double(10.0) required: Option[Double]"

I find this very odd. Why is this happening? Am I not supposed to be allowed to use Doubles for an argument that is defined as Option[Double]?

Answer Source

Of course, for an argument of the type Option[Double], you can not send in the type Double. They are different types.

You can send in Some(20.5) which is of the type Option[Double]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download