out of curiosity, I was wondering if it was possible to do something like :
def myMethod(
a: Option[A] = None,
b: Option[B] = None,
...
z: Option[Z] = N
Possible, yes:
object Test {
implicit def anythingToOption[A](a: A): Option[A] = Option(a)
def foo(something: Option[Int]): Unit = ???
def main(args: Array[String]): Unit = {
foo(1)
}
}
Should you do this? NO. Why? Because implicits with such broad scopes are dangerous. For one, they can lead to ambiguity when you actually need a relevant implicit in scope. Second, when someone reads this, they'll need to see where this conversion happens, and why. Third, this can lead to subtle bugs.
Instead, you can use extension methods, whether you get them from the Cats library or write them yourself:
object Test {
implicit class OptionOps[A](val a: A) extends AnyVal {
def toOption: Option[A] = Option(a)
def some: Option[A] = Some(a)
}
def foo(something: Option[Int]): Unit = ???
def main(args: Array[String]): Unit = {
foo(1.toOption)
foo(1.some)
}
}