Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, how come `println(1,2)` works?

In Scala (2.7.7final), the Predef.println method is defined as having the following signature:

def println (x : Any) : Unit

How come, then that the following works:

scala> println(1,2)  
(1,2)

Does the compiler automatically convert a comma-separated list of arguments into a Tuple? By what magic? Is there an implicit conversion going on here, and if so, which one?

like image 943
lindelof Avatar asked Feb 04 '23 03:02

lindelof


1 Answers

Yes, the compiler will attempt to convert comma separated arguments into tuples, if there are no appropriate multi-argument methods and a single appropriate one-argument method. It's not an implicit conversion, just a compiler hack. This is a somewhat controversial feature, and will probably undergo changes going forward, as work is planned around unifying the treatment of tuples and argument lists.

like image 103
Dave Griffith Avatar answered Feb 14 '23 18:02

Dave Griffith