nsbr11 nsbr11 - 1 month ago 4
Scala Question

How to return None when parameter is double?

Okay, so I am a beginner in Scala and I am required to return None in an if-else function. But I will always get this error:

<console>:42: error: type mismatch;
found : None.type
required: Double
return None


is there a way for me to solve this?

update: these are my codes

def get_grid_parameters(params: Double): Double = {


if (params == "edge_lating") {
return grid_hex_edge_length_latlng
}

else if (params == "grid_bottom_left_lat") {
return grid_bottom_left_lat
}
else if (params == "grid_bottom_left_lng") {
return grid_bottom_left_lng
}
else if (params == "edge_metres") {
return latlng_diff_to_metres(grid_hex_edge_length_latlng)
}
else if (params == "hex_height") {
return 0.002598076211353316
}
else if (params == "increment_lng") {
return 0.002250
}
else if (params == "increment_lat") {
return 0.001299038105676658
}
else {
print("Invalid parameters!")
return None
}

}

Answer

Change the return type of the function to Option[Double]. return is optional in Scala. params should be String

def get_grid_parameters(params: String): Option[Double] = {


if (params == "edge_lating") {
   Some(grid_hex_edge_length_latlng)
}

   else if (params == "grid_bottom_left_lat") {
       Some(grid_bottom_left_lat)
}
   else if (params == "grid_bottom_left_lng") {
       Some(grid_bottom_left_lng)
}
   else if (params == "edge_metres") {
       Some(latlng_diff_to_metres(grid_hex_edge_length_latlng))
}
   else if (params == "hex_height") {
       Some(0.002598076211353316)
}
   else if (params == "increment_lng") {
       Some(0.002250)
}
   else if (params == "increment_lat") {
       Some(0.001299038105676658)
}
   else {
       print("Invalid parameters!")
       return None
}

}

You can use pattern matching its more clear

def getGridParams(param: String): Option[Double] = param match {
 case "edge_lating" => Some(grid_hex_edge_length_latlng)
 case "grid_bottom_left_lat" => Some(grid_bottom_left_lat)
 case "grid_bottom_left_lng" => Some(grid_bottom_left_lng)
 case "edge_metres" => Some(latlng_diff_to_metres(grid_hex_edge_length_latlng))
 case "hex_height" =>  Some(0.002598076211353316)
 case "increment_lng" =>  Some(0.002250)
 case "increment_lat" => Some(0.001299038105676658)
 case _ => None
}

explanation:

In Scala no value is captured using None and some value case is modelled using Some(value). Option is the super type None and Some. So Some and None values can be directly assigned to the variable whose type if Option[T].

Example:

scala> val a: Option[Int] = None
a: Option[Int] = None

scala> val a: Option[Int] = Some(1)
a: Option[Int] = Some(1)

scala> def toInt(str: String): Option[Int] = {
     |   if (str.forall(_.isDigit)) Some(str.toInt)
     |   else None
     | }
toInt: (str: String)Option[Int]

scala> toInt("123a")
res0: Option[Int] = None

scala> toInt("123")
res1: Option[Int] = Some(123)

In the above example if string contains chars we return None if not we return Some(value). Look at the return type of toInt which is Option[Int]

Comments