1

How does the following code work in Scala ?

scala> Some(2,true,3, false)
res13: Some[(Int, Boolean, Int, Boolean)] = Some((2,true,3,false))

I don't see an apply method defined for some which can take more than one argument.

aakashns
  • 115
  • 5
  • 1
    see also http://stackoverflow.com/questions/5997553/why-and-how-is-scala-treating-a-tuple-specially-when-calling-a-one-arg-function http://stackoverflow.com/questions/3526156/in-scala-how-come-println1-2-works http://stackoverflow.com/questions/2850902/scala-coalesces-multiple-function-call-parameters-into-a-tuple-can-this-be-di/2852147#2852147 – stew Jan 03 '15 at 17:28
  • thanks for the references! This is quite surprising. – aakashns Jan 03 '15 at 17:36

2 Answers2

3

Multiple arguments are implicitly adapted to make a tuple if they don't otherwise fit; this will happen for any method, there's nothing special about Some here:

def doSomething(a: Any) = {...}
doSomething(2, true, 3, false)

You can (and, I would argue, should; it's a misfeature that tends to mask programming errors) make the compiler emit a warning if this happens, by passing the flag -Ywarn-adapted-args. See here for a list of similar recommended flags.

lmm
  • 17,386
  • 3
  • 26
  • 37
1

Some(2,true,3, false) is syntactic sugar for Some(new Tuple4(2,true,3, false)).

That's why the type is Some[(Int, Boolean, Int, Boolean)]

It works until 22 elements and if you need more, then you can use a collection, not a tuple.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118