1
votes

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]?

1
It may be useful to explicitly point out here that Option is not a special language feature. It is an ordinary type.Chris Martin

1 Answers

6
votes

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]