The problem is using return in your code. You said you use this piece of code in some function. What is the return type of this function? Obviously, you assume that sometimes it is Integer, sometimes String, sometimes BigDecimal; but if you use return , it will look for the type of the returned object to determine the return type of the function. In general, you should avoid using return in Scala code. The last evaluated value in the function body is returned. The only use case for return is when you want to force a return somewhere else to the function body. Nevertheless, the best way is to save the returned object in a variable and simply evaluate this variable in the last line of your function body. And never use a refund!
Without return it works
scala> val datatype = DecimalType(10, 2) datatype: org.apache.spark.sql.types.DecimalType = DecimalType(10,2) scala> val value = BigDecimal(10) value: scala.math.BigDecimal = 10 scala> datatype match {case DecimalType(_,_) => value} res150: scala.math.BigDecimal = 10
** Return Issues **
scala> def test = {datatype match {case DecimalType(_,_) => return value}} <console>:138: error: method test has return statement; needs result type def test = {datatype match {case DecimalType(_,_) => return value}} scala> def test:BigDecimal = {datatype match {case DecimalType(_,_) => return value}} test: BigDecimal scala> def test:DataType = {datatype match {case DecimalType(_,_) => return value}} <console>:138: error: type mismatch; found : scala.math.BigDecimal required: org.apache.spark.sql.types.DataType def test:DataType = {datatype match {case DecimalType(_,_) => return value}} scala> def test3 = {datatype match {case DecimalType(_,_) => value}} test3: scala.math.BigDecimal
source share