What is the difference between passing two arguments vs passing one argument?
val option1 = Option(String,String)
val option2 = Option(String)
When you write something like
Option(1, 2), the compiler first desugars this to
Option.apply(1, 2), and then when it sees that the
Option companion object doesn't have an
apply method that takes two arguments, it automatically converts the arguments into a tuple:
scala> Option(1, 2) res0: Option[(Int, Int)] = Some((1,2))
It would do something similar for
Option(1, 2, 3),
Option(1, 2, 3, 4), etc.
This is known as auto-tupling and only works for methods with a single argument. For example, the following won't compile:
scala> def foo[T](t: T, u: T): T = t foo: [T](t: T, u: T)T scala> foo(1, 2, 3) <console>:13: error: too many arguments for method foo: (t: T, u: T)T foo(1, 2, 3) ^
This "feature" is provided for syntactic convenience, and it brings Scala a little closer (at least in a superficial way) to other functional languages where tuples and function argument lists are more unified. Lots of people hate auto-tupling, though, because these things aren't actually unified in Scala, and pretending they are can lead to confusing code and annoying error messages. If you're one of these people (I am), you can turn on the
-Ywarn-adapted-args compiler flag, which gives you warnings when the compiler tries to do this:
scala> Option(1, 2) <console>:12: warning: Adapting argument list by creating a 2-tuple: this may not be what you want. signature: Option.apply[A](x: A): Option[A] given arguments: 1, 2 after adaptation: Option((1, 2): (Int, Int)) Option(1, 2) ^ res0: Option[(Int, Int)] = Some((1,2))
This is a matter of taste, though.