简体   繁体   中英

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

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM