[英]Scala: Why do I get a type mismatch error when I use “Option”?
I'm new to Scala. 我是Scala的新手。 I have a Scala function, and one of its arguments uses "Option": 我有一个Scala函数,其中一个参数使用“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]" 但这会导致类型不匹配错误:“类型不匹配;找到:需要Double(10.0):选项[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]? 我不应该被允许使用Doubles作为定义为Option [Double]的参数吗?
Of course, for an argument of the type Option[Double]
, you can not send in the type Double
. 当然,对于Option[Double]
类型的参数,您不能发送Double
类型。 They are different types. 它们是不同的类型。
You can send in Some(20.5)
which is of the type Option[Double]
您可以发送Some(20.5)
类型Option[Double]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.