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.
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.
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.
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.