Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Contrary to <a href="https://stackoverflow.com/questions/2850902/scala-coalesces-multiple-function-call-parameters-into-a-tuple-can-this-be-dis/2851212#2851212">this explanation</a>, Scala <em>parses</em> <code>println(1,2)</code> (or <code>Console println (1,2)</code> for that matter) the same way it parses any two-argument method call. Later, the compiler transforms the call by wrapping the method arguments in a tuple to match the actual method type signature.</p> <p>If the compiler did not do this, perfectly valid expressions like <code>Console println (1,2)</code> would fail to compile because <code>println</code> does not take multiple arguments. There are also <a href="https://stackoverflow.com/questions/5985076/scala-generic-class-supporting-function-of-multiple-arities">other valid use cases</a> for this behavior.</p> <p>Consider an expression like <code>foo bar (1,2)</code> from the compiler's point of view, keeping in mind that Scala has special syntax that allows you to drop the <code>.</code> and the parens on method calls. This could be a call to a two-argument <code>bar</code> method with arguments <code>1</code> and <code>2</code>, or it could be a call to a one-argument <code>bar</code> method with a single tuple-valued argument. The parser doesn't know anything about the <code>bar</code> method, so it just parses as a two-argument method call. </p> <p>During the type checking phase, suppose the compiler determines that <code>foo</code> has no two-argument <code>bar</code> method but that it does have a one-argument <code>bar</code> method whose signature is compatible with the tuple interpretation. Since there is no other valid interpretation, it assumes that this is what you meant and transforms the two arguments into a tuple. Note that if there is a two-argument <code>bar</code> method, even one that is incompatible with the actual arguments, the typer <em>will not</em> perform the auto-tupling transformation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload