0

I am just a beginner in scala and I am learning about the functions. I just developed a small piece of code as below.

class OptionClass {
}
object OptionClass {
  def main(args: Array[String]) {
    println("This is my third code")
    def add(a: Int, c: Int, d: Option[Int]): Int = {
      d match {
        case Some(d) =>
          val f = a + c + d
          f
        case None => a + c

      }
    }

    val result = add(1, 2, None)
    println(s"The result value is $result")

    val newResult = add(1, 2, Some(5))
    println(s"The new result is $newResult")

    val revisedResult = add(1, 2)
    println(s"The result value is $revisedResult")

   //****Adding the code snippets as per the comments below as it may be  useful for someone else who refers the questio
    def addVal(a:Int,b:Option[Int]= None):Int = {
      a+b.getOrElse(10)
    }


val firstResult=addVal(1)
println(s"The firstResult is $firstResult")

val secondResult=addVal(1,Some(2))
println(s"The secondResult is $secondResult")

val thirdResult=addVal(1,None)
println(s"The thirdResult is $thirdResult")




  }
}

My question is here is,

When I called the function using add(1,2,None)) and add(1,2,Some(5)) the code works, I tried calling the function using add(1,2) and code has issues. Even though I have mentioned the third argument is option, why should we mention the third argument as None or Some(d) (is some(d) is the right way to pass it?). It would be very helpful if you suggest the correct way to use the Option according to this code.

Karthi
  • 708
  • 1
  • 19
  • 38
  • Having an argument of type `Option` doesn't make that argument optional to provide. That's not what `Option` is for. Are you just looking to have optional arguments? – Carcigenicate Apr 29 '18 at 17:50
  • Yeah I am just looking to have optional arguments. Also I would like to know if add(1, 2, Some(5)) is right way to call. – Karthi Apr 29 '18 at 17:51
  • As a rule of thumb, avoid `Option` parameters or fields: [they often carry with them accidental complexity](https://stackoverflow.com/questions/42042263/is-using-optional-in-scalas-case-classes-and-classes-fields-a-code-smell/42042981#42042981). Using a default parameter is much simpler to understand and use, here. – jub0bs Apr 29 '18 at 20:32

2 Answers2

3

Options aren't to allow parameters to be omitted from a function call; they're for when a variable may have a concrete value (Some), or may lack a concrete value (None).

If you want to allow d to be omitted from the call, just make it a default parameter. I'm just going to default it to 0 since that won't do anything when added:

//                      Default d to 0
def add(a: Int, c: Int, d: Int = 0): Int =
  a + c + d

add(1, 2) // 3
add(1, 2, 3) // 6
Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Thank you very much for your answer. I get it now. I was misleaded by the name "Option" though. – Karthi Apr 29 '18 at 18:07
  • @Karthi They're called `Option`s because they may optionally be Some or None. They *could* be used in optional parameters as the other answer shows, but I think thats just complicating things more than necessary. – Carcigenicate Apr 29 '18 at 18:09
1

The way in Scala to have default value for parameter is as follow: (doesn’t matter the type)

def foo(a: Int, b: Option[Int] = None) = {
   a + b.getOrElse(0)
}
foo(1, Some(2)) // compiles equals 3
foo(1, None) // compiles equals 1
foo(1) // compiles equals 1
Noam Shaish
  • 1,613
  • 2
  • 16
  • 37
  • The `Optional` parameter makes the method hard to use, though. Choose `Int` for the type of `b` and use a default value of `0`, and users of `foo` will thank you. – jub0bs Apr 29 '18 at 20:35
  • 1
    That was for the sake of the example, we might also agree that foo is a horrible name for a method – Noam Shaish Apr 29 '18 at 20:36