Note that there are some explanatory texts on larger screens.

plurals
  1. POInvoking scala.Function.tupled() from Java
    primarykey
    data
    text
    <p>I would like to express all of the following Scala code in Java:</p> <pre><code>object TupleDemo { val tuple = (3, "Hello, world! ") /** @return str replicated x times */ def foo(x: Int, str: String) = str * x val tupledFoo1 = (foo _).tupled // partially applied function val tupledFoo2 = Function.tupled(foo _) // desugared syntax for same partially applied function } object TupleDemoApp extends App { import TupleDemo._ println(tupledFoo1(tuple)) // Hello, world! Hello, world! Hello, world! println(tupledFoo2(tuple)) // Hello, world! Hello, world! Hello, world! } </code></pre> <p>This is as much as I could figure out for the Java equivalent:</p> <pre><code>import scala.Function1; import scala.Function2; import scala.Tuple2; import scala.collection.immutable.WrappedString; import scala.runtime.AbstractFunction2; public class JavaTupleDemo { /** @return str replicated x times */ static final Function2&lt;Integer, String, String&gt; foo = new AbstractFunction2&lt;Integer, String, String&gt;() { public String apply(Integer x, String str) { return new WrappedString(str).$times(x); } // perhaps the types for this method are incorrect? public Function1&lt;Tuple2&lt;Integer, String&gt;, String&gt; tupled(Tuple2 tuple2) { return null; // what to write here instead of null? } }; public static void main(String[] args) { // works: Invoke tupled function defined in Scala from Java using Tuple2 defined in Java Tuple2&lt;Object, String&gt; tuple = new Tuple2&lt;Object, String&gt;(3, "Hello, World! "); System.out.println(TupleDemo.tupledFoo1().apply(tuple)); // works: Invoke regular function defined in Java from Java System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! ")); // stumped: Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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