错误:(63,3)类型不匹配;找到:所需单位:双

时间:2018-09-26 03:20:30

标签: scala apache-spark apache-spark-sql

val Match: (String) => Double = (address: String) => {
    val address =
    if (address == null)
      empty
    else
      Address
}

在此代码中,我遇到此错误

  
    

类型不匹配;找到:所需单位:双倍

  

2 个答案:

答案 0 :(得分:1)

Match函数的返回类型是一个接受string并返回double的函数

(String) => Double

您在评论中提到

  
    

val空= 0.2,val地址= 0.0

  

因此正确的函数定义应为

val Match: (String) => Double = (address: String) => {
   if (address == null)
      empty
    else
      Address
}

错误是因为您没有从i f else表达式中计算出的函数中返回双精度值,并且在范围内使用了与输入参数相同的变量名函数val address =

或者,您也可以使用赋值并返回该赋值

val Match: (String) => Double = (address: String) => {
   val addr = if (address == null)
      empty
    else
      Address
  addr
}

我希望答案会有所帮助

答案 1 :(得分:0)

或者,

val empty = 0.2; val Address = 0.0

val Match: (String) => Double =
 (address:String)=>address match{
 case null => empty
 case _ => Address
 }

在Scala REPL中:

scala> Match(null)
res79: Double = 0.2

scala> Match("")
res80: Double = 0.0

scala> Match("yoohh")
res81: Double = 0.0