Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem with the answer by Travis Brown is that he did not include all of the code. Here is the complete code, which does not compile.</p> <pre><code>import scala.Function; import scala.Function2; import scala.Tuple2; import scala.collection.immutable.WrappedString; import scala.runtime.AbstractFunction2; public class JavaTupleDemo { /** tupled() is implemented in Function2 (and therefore in AbstractFunction2) * @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); } }; public static void main(String[] args) { // 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)); // Invoke regular function defined in Java from Java System.out.println(JavaTupleDemo.foo.apply(3, "Hello, planet! ")); // Invoke tupled function defined in Java from Java using both the Scala and the Java Tuple2 instances System.out.println(JavaTupleDemo.foo.tupled().apply(tuple)); System.out.println(Function.tupled(JavaTupleDemo.foo).apply(tuple)); } } </code></pre> <p>The error messages are:</p> <pre><code>error: method apply in interface Function1&lt;T1,R&gt; cannot be applied to given types; required: Tuple2&lt;Integer,String&gt; found: Tuple2&lt;Object,String&gt; reason: actual argument Tuple2&lt;Object,String&gt; cannot be converted to Tuple2&lt;Integer,String&gt; by method invocation conversion where T1,R are type-variables: T1 extends Object declared in interface Function1 R extends Object declared in interface Function1 </code></pre> <p>You cannot simply cast tuple to a Tuple2&lt;Integer, String>. If you try the following:</p> <pre><code>System.out.println(JavaTupleDemo.foo.tupled().apply((Tuple2&lt;Integer, String&gt;)tuple)); System.out.println(Function.tupled(JavaTupleDemo.foo).apply((Tuple2&lt;Integer, String&gt;)tuple)); </code></pre> <p>The error message is:</p> <pre><code>error: inconvertible types required: Tuple2&lt;Integer,String&gt; found: Tuple2&lt;Object,String&gt; </code></pre> <p>The only way to make this compile is to do something really nasty - cast tuple first to an Object, then to a Tuple2&lt;Integer, String>:</p> <pre><code>System.out.println(JavaTupleDemo.foo.tupled().apply((Tuple2&lt;Integer, String&gt;)((Object)javaTuple))); System.out.println(Function.tupled(JavaTupleDemo.foo).apply((Tuple2&lt;Integer, String&gt;)((Object)javaTuple))); </code></pre> <p>Is there a better way to do this?</p>
    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.
    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