This page is no longer maintained — Please continue to the home page at www.scala-lang.org

java interop with Scala tuples

6 replies
Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.

Suppose I have a Scala function that returns a tuple, and it will be
called from Java. Is there a way to make the Java code understand the
Scala tuple? Thanks.

--Russ P.

Michael Schmitz
Joined: 2011-11-01,
User offline. Last seen 42 years 45 weeks ago.
Re: java interop with Scala tuples

Yes, you just get an instance of the TupleN class. For example, if
you return (1, "cow") you will get a type of Tuple2
in Java.

Peace. Michael

On Mon, Nov 28, 2011 at 4:01 PM, Russ P. wrote:
> Suppose I have a Scala function that returns a tuple, and it will be
> called from Java. Is there a way to make the Java code understand the
> Scala tuple? Thanks.
>
> --Russ P.
>

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: java interop with Scala tuples

Are the TupleN classes something I need to create myself, or do they
already exist in the standard Java libraries?

I found this:

http://gleichmann.wordpress.com/2008/01/15/building-your-own-literals-in...

Is this what I need?

--Russ P.

On Nov 28, 4:03 pm, Michael Schmitz wrote:
> Yes, you just get an instance of the TupleN class.  For example, if
> you return (1, "cow") you will get a type of Tuple2
> in Java.
>
> Peace.  Michael
>
> On Mon, Nov 28, 2011 at 4:01 PM, Russ P. wrote:
> > Suppose I have a Scala function that returns a tuple, and it will be
> > called from Java. Is there a way to make the Java code understand the
> > Scala tuple? Thanks.
>
> > --Russ P.

Michael Schmitz
Joined: 2011-11-01,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: java interop with Scala tuples

http://www.scala-lang.org/api/current/scala/Tuple2.html
http://www.scala-lang.org/api/current/scala/Tuple3.html

etc.

This blog has an example of what you want.

http://www.codecommit.com/blog/java/interop-between-java-and-scala

Peace. Michael

On Mon, Nov 28, 2011 at 5:05 PM, Russ P. wrote:
> Are the TupleN classes something I need to create myself, or do they
> already exist in the standard Java libraries?
>
> I found this:
>
> http://gleichmann.wordpress.com/2008/01/15/building-your-own-literals-in...
>
> Is this what I need?
>
> --Russ P.
>
>
> On Nov 28, 4:03 pm, Michael Schmitz wrote:
>> Yes, you just get an instance of the TupleN class.  For example, if
>> you return (1, "cow") you will get a type of Tuple2
>> in Java.
>>
>> Peace.  Michael
>>
>> On Mon, Nov 28, 2011 at 4:01 PM, Russ P. wrote:
>> > Suppose I have a Scala function that returns a tuple, and it will be
>> > called from Java. Is there a way to make the Java code understand the
>> > Scala tuple? Thanks.
>>
>> > --Russ P.
>

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: java interop with Scala tuples

I'm making some progress here, but now it's telling me that the
elements of the tuple (_1, _2, etc.) are private:

[error] /home/code/src/test/java/JavaInteropTest.java:24: _1 has
private access in scala.Tuple5
[error] java.lang.Double time = result._1;

What is the problem here? That can't be right, can it?

Here is the relevant code:

scala.Tuple5,
scala.collection.immutable.Set,
scala.collection.immutable.List,
scala.collection.immutable.Set>
result = tsafe.updateTracks(100, "AAL123", state, 99);

java.lang.Double time = result._1;
scala.collection.immutable.List newConflicts =
result._2;
scala.collection.immutable.Set removed =
result._3;

--Russ P.

On Nov 28, 5:07 pm, Michael Schmitz wrote:
> http://www.scala-lang.org/api/current/scala/Tuple2.htmlhttp://www.scala-...
>
> etc.
>
> This blog has an example of what you want.
>
> http://www.codecommit.com/blog/java/interop-between-java-and-scala
>
> Peace.  Michael
>
> On Mon, Nov 28, 2011 at 5:05 PM, Russ P. wrote:
> > Are the TupleN classes something I need to create myself, or do they
> > already exist in the standard Java libraries?
>
> > I found this:
>
> >http://gleichmann.wordpress.com/2008/01/15/building-your-own-literals...
>
> > Is this what I need?
>
> > --Russ P.
>
> > On Nov 28, 4:03 pm, Michael Schmitz wrote:
> >> Yes, you just get an instance of the TupleN class.  For example, if
> >> you return (1, "cow") you will get a type of Tuple2
> >> in Java.
>
> >> Peace.  Michael
>
> >> On Mon, Nov 28, 2011 at 4:01 PM, Russ P. wrote:
> >> > Suppose I have a Scala function that returns a tuple, and it will be
> >> > called from Java. Is there a way to make the Java code understand the
> >> > Scala tuple? Thanks.
>
> >> > --Russ P.

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: Re: java interop with Scala tuples
You have to use the accessor methods _1() and so on.
  --Rex

On Mon, Nov 28, 2011 at 8:41 PM, Russ P. <russ.paielli@gmail.com> wrote:
I'm making some progress here, but now it's telling me that the
elements of the tuple (_1, _2, etc.) are private:

[error] /home/code/src/test/java/JavaInteropTest.java:24: _1 has
private access in scala.Tuple5
[error]         java.lang.Double time = result._1;

What is the problem here? That can't be right, can it?

Here is the relevant code:

       scala.Tuple5<Double,
           scala.collection.immutable.List<Encounter>,
           scala.collection.immutable.Set<java.lang.String>,
           scala.collection.immutable.List<Maneuvers>,
           scala.collection.immutable.Set<java.lang.String>>
           result = tsafe.updateTracks(100, "AAL123", state, 99);

       java.lang.Double time = result._1;
       scala.collection.immutable.List<Encounter> newConflicts =
result._2;
       scala.collection.immutable.Set<java.lang.String> removed =
result._3;

--Russ P.


On Nov 28, 5:07 pm, Michael Schmitz <mich...@schmitztech.com> wrote:
> http://www.scala-lang.org/api/current/scala/Tuple2.htmlhttp://www.scala-lang.org/api/current/scala/Tuple3.html
>
> etc.
>
> This blog has an example of what you want.
>
> http://www.codecommit.com/blog/java/interop-between-java-and-scala
>
> Peace.  Michael
>
> On Mon, Nov 28, 2011 at 5:05 PM, Russ P. <russ.paie...@gmail.com> wrote:
> > Are the TupleN classes something I need to create myself, or do they
> > already exist in the standard Java libraries?
>
> > I found this:
>
> >http://gleichmann.wordpress.com/2008/01/15/building-your-own-literals...
>
> > Is this what I need?
>
> > --Russ P.
>
> > On Nov 28, 4:03 pm, Michael Schmitz <mich...@schmitztech.com> wrote:
> >> Yes, you just get an instance of the TupleN class.  For example, if
> >> you return (1, "cow") you will get a type of Tuple2<Integer, String>
> >> in Java.
>
> >> Peace.  Michael
>
> >> On Mon, Nov 28, 2011 at 4:01 PM, Russ P. <russ.paie...@gmail.com> wrote:
> >> > Suppose I have a Scala function that returns a tuple, and it will be
> >> > called from Java. Is there a way to make the Java code understand the
> >> > Scala tuple? Thanks.
>
> >> > --Russ P.

Russ P.
Joined: 2009-01-31,
User offline. Last seen 1 year 26 weeks ago.
Re: java interop with Scala tuples

On Nov 28, 6:18 pm, Rex Kerr wrote:
> You have to use the accessor methods _1() and so on.
>   --Rex
>

Thanks. Actually, I just realized that I should use a simple case
class rather than a tuple. Hey, better late than never.

--Russ P.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland