Reputation: 1069
What is the difference between passing two arguments vs passing one argument?
val option1 = Option(String,String)
and
val option2 = Option(String)
Upvotes: 1
Views: 361
Reputation: 139028
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.
Upvotes: 8
Reputation: 369420
There is no overload of Option.apply
with two arguments, so the simple difference between the two is that the first one gives an error related to the number of arguments:
error: too many arguments (2) for method apply: (x: A)Option[A] in object Option
Whereas the second one gives an error because String
is a type, not a value, and you are trying to pass a type as an argument, which is impossible:
error: object java.lang.String is not a value
Upvotes: 1