- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: What does my Java class need to implement so I can use it in a Scala for-comprehension?
Thu, 2010-10-07, 10:56
Oops, of course I meant:
standard lib:
scala> for { x <- a } yield x
res0: scala.collection.mutable.Buffer[String] = ArrayBuffer()
scalaz:
scala> for { x <- a } yield x
res1: java.util.ArrayList[String] = []
On Thu, Oct 7, 2010 at 11:54 AM, Jason Zaugg wrote:
> With Standard Library implicit conversions:
>
> scala> import collection.JavaConversions._
> import collection.JavaConversions._
>
> scala> for { x <- a } yield a
> res0: scala.collection.mutable.Buffer[java.util.ArrayList[String]] = ArrayBuffer
> ()
>
>
> Or with scalaz. Notice the return type of the for-comprehension is a
> new ArrayList.
>
> scala> val a = new java.util.ArrayList[String]
> a: java.util.ArrayList[String] = []
>
> scala> import scalaz._; import Scalaz._
> import scalaz._
> import Scalaz._
>
> scala> for { x <- a } yield a
> res0: java.util.ArrayList[java.util.ArrayList[String]] = []
>
>
> On Thu, Oct 7, 2010 at 11:47 AM, mal3 wrote:
>>
>> This is a Scala calling Java interoperability question.
>>
>> I've looked through three Scala books and Googled for an answer, but nothing
>> specific came up,
>> so I hope I'm not asking a trivial question.
>>
>> I have a Java class, which I wrote and therefore can modify.
>>
>> The Java class wraps a Java ArrayList.
>>
>> I have a Scala class in which I use my Java class, and it works.
>>
>> I would like to use my Java class in a for-comprehension in my Scala class.
>>
>> What methods or interfaces do I need to implement in my Java class so that I
>> can use it in a Scala for-comprehension?
>>
>> Regards, Mal.
>> --
>> View this message in context: http://scala-programming-language.1934581.n4.nabble.com/What-does-my-Jav...
>> Sent from the Scala - User mailing list archive at Nabble.com.
>>
>
Thu, 2010-10-07, 13:07
#2
Re: What does my Java class need to implement so I can use it i
I interpreted the OP's question as follows:
Which methods do I have to implement in my custom Java class to enable
it for for comprehensions?
Paragraph 6.19 in the SLS [1] answers how for comprehensions are
translated to method invocations.
Example:
File: CustomCollection.java
import java.util.ArrayList;
import java.util.List;
import scala.Function1;
public class CustomCollection {
private List wrapped;
public CustomCollection(List a) { wrapped = a; }
public CustomCollection map(Function1 f) {
List mapped = new ArrayList(wrapped.size());
for (A a : wrapped) {
mapped.add(f.apply(a));
}
return new CustomCollection(mapped);
}
@Override
public String toString() {
return "CustomCollection: " + wrapped.toString();
}
}
File: Test.scala
import forjava.CustomCollection
object Test {
def main(args: Array[String]) {
val wrapped = new java.util.ArrayList[Int]();
wrapped.add(1); wrapped.add(2); wrapped.add(3);
val cc = new CustomCollection(wrapped)
val ccplus1 = for (c <- cc) yield { c + 1 };
println(ccplus1)
}
}
Prints: CustomCollection: [2, 3, 4]
The example shows only what is required for a single generator.
[1] http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/fi...
On Thu, Oct 7, 2010 at 11:56 AM, Jason Zaugg wrote:
> Oops, of course I meant:
>
> standard lib:
>
> scala> for { x <- a } yield x
> res0: scala.collection.mutable.Buffer[String] = ArrayBuffer()
>
> scalaz:
>
> scala> for { x <- a } yield x
> res1: java.util.ArrayList[String] = []
>
> On Thu, Oct 7, 2010 at 11:54 AM, Jason Zaugg wrote:
>> With Standard Library implicit conversions:
>>
>> scala> import collection.JavaConversions._
>> import collection.JavaConversions._
>>
>> scala> for { x <- a } yield a
>> res0: scala.collection.mutable.Buffer[java.util.ArrayList[String]] = ArrayBuffer
>> ()
>>
>>
>> Or with scalaz. Notice the return type of the for-comprehension is a
>> new ArrayList.
>>
>> scala> val a = new java.util.ArrayList[String]
>> a: java.util.ArrayList[String] = []
>>
>> scala> import scalaz._; import Scalaz._
>> import scalaz._
>> import Scalaz._
>>
>> scala> for { x <- a } yield a
>> res0: java.util.ArrayList[java.util.ArrayList[String]] = []
>>
>>
>> On Thu, Oct 7, 2010 at 11:47 AM, mal3 wrote:
>>>
>>> This is a Scala calling Java interoperability question.
>>>
>>> I've looked through three Scala books and Googled for an answer, but nothing
>>> specific came up,
>>> so I hope I'm not asking a trivial question.
>>>
>>> I have a Java class, which I wrote and therefore can modify.
>>>
>>> The Java class wraps a Java ArrayList.
>>>
>>> I have a Scala class in which I use my Java class, and it works.
>>>
>>> I would like to use my Java class in a for-comprehension in my Scala class.
>>>
>>> What methods or interfaces do I need to implement in my Java class so that I
>>> can use it in a Scala for-comprehension?
>>>
>>> Regards, Mal.
>>> --
>>> View this message in context: http://scala-programming-language.1934581.n4.nabble.com/What-does-my-Jav...
>>> Sent from the Scala - User mailing list archive at Nabble.com.
>>>
>>
>
Thu, 2010-10-07, 14:27
#3
Re: What does my Java class need to implement so I can use it i
After skimming through the Programmig in Scala Book (Odersky & co.)
chapter 23.6 "Generalizing for" I tried to enable a custom Java class
for full for comprehension support and hit a strange problem.
Here is what works so far:
object Test {
def main(args: Array[String]) {
val wrapped = new java.util.ArrayList[Int]();
wrapped.add(1); wrapped.add(2); wrapped.add(3);
val cc = new CustomCollection(wrapped)
println(for { c <- cc} yield { c + 1 })
println(for { c1 <- cc; c2 <- cc} yield {c1 + c2 + 1})
println(for { c <- cc if c > 1} yield { c + 1 })
// for(i <- cc){ println("") } // DOES NOT WORK
}
}
import scala.Function1;
import scala.runtime.Unit;
public class CustomCollection {
private final List wrapped;
public CustomCollection(List a) {
wrapped = a;
}
public CustomCollection map(Function1 f) {
List mapped = new ArrayList(wrapped.size());
for (A a : wrapped) {
mapped.add(f.apply(a));
}
return new CustomCollection(mapped);
}
public CustomCollection flatMap(Function1> f) {
List flattened = new ArrayList();
for (A a : wrapped) {
flattened.addAll(f.apply(a).wrapped);
}
return new CustomCollection(flattened);
}
public CustomCollection withFilter(Function1 p) {
List filtered = new ArrayList();
for (A a : wrapped) {
if (p.apply(a)) {
filtered.add(a);
}
}
return new CustomCollection(filtered);
}
// does NOT work as expected!
public void foreach(Function1 b) {
for (A a : wrapped) {
b.apply(a);
}
}
@Override
public String toString() {
return "CustomCollection: " + wrapped.toString();
}
}
prints:
CustomCollection: [2, 3, 4]
CustomCollection: [3, 4, 5, 4, 5, 6, 5, 6, 7]
CustomCollection: [3, 4]
But when uncommenting the line
// for(i <- cc){ println("") }
I get the following compile error:
forcomp/Test.scala:9: error: type mismatch;
found : scala.Unit
required: scala.runtime.Unit
for(i <- cc){ println(""); }
Any ideas what goes wrong here?
On Thu, Oct 7, 2010 at 1:51 PM, Daniel Kröni wrote:
> I interpreted the OP's question as follows:
> Which methods do I have to implement in my custom Java class to enable
> it for for comprehensions?
>
> Paragraph 6.19 in the SLS [1] answers how for comprehensions are
> translated to method invocations.
>
> Example:
>
> File: CustomCollection.java
>
> import java.util.ArrayList;
> import java.util.List;
> import scala.Function1;
>
> public class CustomCollection {
> private List wrapped;
> public CustomCollection(List a) { wrapped = a; }
> public CustomCollection map(Function1 f) {
> List mapped = new ArrayList(wrapped.size());
> for (A a : wrapped) {
> mapped.add(f.apply(a));
> }
> return new CustomCollection(mapped);
> }
>
> @Override
> public String toString() {
> return "CustomCollection: " + wrapped.toString();
> }
> }
>
> File: Test.scala
>
> import forjava.CustomCollection
> object Test {
> def main(args: Array[String]) {
> val wrapped = new java.util.ArrayList[Int]();
> wrapped.add(1); wrapped.add(2); wrapped.add(3);
> val cc = new CustomCollection(wrapped)
>
> val ccplus1 = for (c <- cc) yield { c + 1 };
> println(ccplus1)
> }
> }
>
> Prints: CustomCollection: [2, 3, 4]
>
> The example shows only what is required for a single generator.
>
>
> [1] http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/fi...
>
> On Thu, Oct 7, 2010 at 11:56 AM, Jason Zaugg wrote:
>> Oops, of course I meant:
>>
>> standard lib:
>>
>> scala> for { x <- a } yield x
>> res0: scala.collection.mutable.Buffer[String] = ArrayBuffer()
>>
>> scalaz:
>>
>> scala> for { x <- a } yield x
>> res1: java.util.ArrayList[String] = []
>>
>> On Thu, Oct 7, 2010 at 11:54 AM, Jason Zaugg wrote:
>>> With Standard Library implicit conversions:
>>>
>>> scala> import collection.JavaConversions._
>>> import collection.JavaConversions._
>>>
>>> scala> for { x <- a } yield a
>>> res0: scala.collection.mutable.Buffer[java.util.ArrayList[String]] = ArrayBuffer
>>> ()
>>>
>>>
>>> Or with scalaz. Notice the return type of the for-comprehension is a
>>> new ArrayList.
>>>
>>> scala> val a = new java.util.ArrayList[String]
>>> a: java.util.ArrayList[String] = []
>>>
>>> scala> import scalaz._; import Scalaz._
>>> import scalaz._
>>> import Scalaz._
>>>
>>> scala> for { x <- a } yield a
>>> res0: java.util.ArrayList[java.util.ArrayList[String]] = []
>>>
>>>
>>> On Thu, Oct 7, 2010 at 11:47 AM, mal3 wrote:
>>>>
>>>> This is a Scala calling Java interoperability question.
>>>>
>>>> I've looked through three Scala books and Googled for an answer, but nothing
>>>> specific came up,
>>>> so I hope I'm not asking a trivial question.
>>>>
>>>> I have a Java class, which I wrote and therefore can modify.
>>>>
>>>> The Java class wraps a Java ArrayList.
>>>>
>>>> I have a Scala class in which I use my Java class, and it works.
>>>>
>>>> I would like to use my Java class in a for-comprehension in my Scala class.
>>>>
>>>> What methods or interfaces do I need to implement in my Java class so that I
>>>> can use it in a Scala for-comprehension?
>>>>
>>>> Regards, Mal.
>>>> --
>>>> View this message in context: http://scala-programming-language.1934581.n4.nabble.com/What-does-my-Jav...
>>>> Sent from the Scala - User mailing list archive at Nabble.com.
>>>>
>>>
>>
>
Thu, 2010-10-07, 14:47
#4
Re: What does my Java class need to implement so I can use it i
i went from for-loops to calling foreach, for example
0 to 10 foreach {...}
i think this way just.... fits better :D
this doesn't solve your problem, but would work if you'd be willing to scrap for-loops
-------- Original-Nachricht --------
> Datum: Thu, 7 Oct 2010 15:24:26 +0200
> Von: "Daniel Kröni"
> An: mal3
> CC: Jason Zaugg , scala-user@listes.epfl.ch
> Betreff: Re: [scala-user] What does my Java class need to implement so I can use it in a Scala for-comprehension?
> After skimming through the Programmig in Scala Book (Odersky & co.)
> chapter 23.6 "Generalizing for" I tried to enable a custom Java class
> for full for comprehension support and hit a strange problem.
> Here is what works so far:
>
> object Test {
> def main(args: Array[String]) {
> val wrapped = new java.util.ArrayList[Int]();
> wrapped.add(1); wrapped.add(2); wrapped.add(3);
> val cc = new CustomCollection(wrapped)
>
> println(for { c <- cc} yield { c + 1 })
> println(for { c1 <- cc; c2 <- cc} yield {c1 + c2 + 1})
> println(for { c <- cc if c > 1} yield { c + 1 })
> // for(i <- cc){ println("") } // DOES NOT WORK
> }
> }
>
> import scala.Function1;
> import scala.runtime.Unit;
>
> public class CustomCollection {
> private final List wrapped;
>
> public CustomCollection(List a) {
> wrapped = a;
> }
>
> public CustomCollection map(Function1 f) {
> List mapped = new ArrayList(wrapped.size());
> for (A a : wrapped) {
> mapped.add(f.apply(a));
> }
> return new CustomCollection(mapped);
> }
>
> public CustomCollection flatMap(Function1>
> f) {
> List flattened = new ArrayList();
> for (A a : wrapped) {
> flattened.addAll(f.apply(a).wrapped);
> }
> return new CustomCollection(flattened);
> }
>
> public CustomCollection withFilter(Function1 p) {
> List filtered = new ArrayList();
> for (A a : wrapped) {
> if (p.apply(a)) {
> filtered.add(a);
> }
> }
> return new CustomCollection(filtered);
> }
>
> // does NOT work as expected!
> public void foreach(Function1 b) {
> for (A a : wrapped) {
> b.apply(a);
> }
> }
>
> @Override
> public String toString() {
> return "CustomCollection: " + wrapped.toString();
> }
> }
>
> prints:
> CustomCollection: [2, 3, 4]
> CustomCollection: [3, 4, 5, 4, 5, 6, 5, 6, 7]
> CustomCollection: [3, 4]
>
> But when uncommenting the line
> // for(i <- cc){ println("") }
> I get the following compile error:
>
> forcomp/Test.scala:9: error: type mismatch;
> found : scala.Unit
> required: scala.runtime.Unit
> for(i <- cc){ println(""); }
>
> Any ideas what goes wrong here?
>
>
>
> On Thu, Oct 7, 2010 at 1:51 PM, Daniel Kröni
> wrote:
> > I interpreted the OP's question as follows:
> > Which methods do I have to implement in my custom Java class to enable
> > it for for comprehensions?
> >
> > Paragraph 6.19 in the SLS [1] answers how for comprehensions are
> > translated to method invocations.
> >
> > Example:
> >
> > File: CustomCollection.java
> >
> > import java.util.ArrayList;
> > import java.util.List;
> > import scala.Function1;
> >
> > public class CustomCollection {
> > private List wrapped;
> > public CustomCollection(List a) { wrapped = a; }
> > public CustomCollection map(Function1 f) {
> > List mapped = new ArrayList(wrapped.size());
> > for (A a : wrapped) {
> > mapped.add(f.apply(a));
> > }
> > return new CustomCollection(mapped);
> > }
> >
> > @Override
> > public String toString() {
> > return "CustomCollection: " + wrapped.toString();
> > }
> > }
> >
> > File: Test.scala
> >
> > import forjava.CustomCollection
> > object Test {
> > def main(args: Array[String]) {
> > val wrapped = new java.util.ArrayList[Int]();
> > wrapped.add(1); wrapped.add(2); wrapped.add(3);
> > val cc = new CustomCollection(wrapped)
> >
> > val ccplus1 = for (c <- cc) yield { c + 1 };
> > println(ccplus1)
> > }
> > }
> >
> > Prints: CustomCollection: [2, 3, 4]
> >
> > The example shows only what is required for a single generator.
> >
> >
> > [1]
> http://www.scala-lang.org/sites/default/files/linuxsoft_archives/docu/fi...
> >
> > On Thu, Oct 7, 2010 at 11:56 AM, Jason Zaugg wrote:
> >> Oops, of course I meant:
> >>
> >> standard lib:
> >>
> >> scala> for { x <- a } yield x
> >> res0: scala.collection.mutable.Buffer[String] = ArrayBuffer()
> >>
> >> scalaz:
> >>
> >> scala> for { x <- a } yield x
> >> res1: java.util.ArrayList[String] = []
> >>
> >> On Thu, Oct 7, 2010 at 11:54 AM, Jason Zaugg wrote:
> >>> With Standard Library implicit conversions:
> >>>
> >>> scala> import collection.JavaConversions._
> >>> import collection.JavaConversions._
> >>>
> >>> scala> for { x <- a } yield a
> >>> res0: scala.collection.mutable.Buffer[java.util.ArrayList[String]] =
> ArrayBuffer
> >>> ()
> >>>
> >>>
> >>> Or with scalaz. Notice the return type of the for-comprehension is a
> >>> new ArrayList.
> >>>
> >>> scala> val a = new java.util.ArrayList[String]
> >>> a: java.util.ArrayList[String] = []
> >>>
> >>> scala> import scalaz._; import Scalaz._
> >>> import scalaz._
> >>> import Scalaz._
> >>>
> >>> scala> for { x <- a } yield a
> >>> res0: java.util.ArrayList[java.util.ArrayList[String]] = []
> >>>
> >>>
> >>> On Thu, Oct 7, 2010 at 11:47 AM, mal3
> wrote:
> >>>>
> >>>> This is a Scala calling Java interoperability question.
> >>>>
> >>>> I've looked through three Scala books and Googled for an answer, but
> nothing
> >>>> specific came up,
> >>>> so I hope I'm not asking a trivial question.
> >>>>
> >>>> I have a Java class, which I wrote and therefore can modify.
> >>>>
> >>>> The Java class wraps a Java ArrayList.
> >>>>
> >>>> I have a Scala class in which I use my Java class, and it works.
> >>>>
> >>>> I would like to use my Java class in a for-comprehension in my Scala
> class.
> >>>>
> >>>> What methods or interfaces do I need to implement in my Java class so
> that I
> >>>> can use it in a Scala for-comprehension?
> >>>>
> >>>> Regards, Mal.
> >>>> --
> >>>> View this message in context:
> http://scala-programming-language.1934581.n4.nabble.com/What-does-my-Jav...
> >>>> Sent from the Scala - User mailing list archive at Nabble.com.
> >>>>
> >>>
> >>
> >
Thu, 2010-10-07, 15:07
#5
Re: What does my Java class need to implement so I can use it
Daniel Kröni wrote:
> forcomp/Test.scala:9: error: type mismatch;
> found : scala.Unit
> required: scala.runtime.Unit
> for(i <- cc){ println(""); }
>
> Any ideas what goes wrong here?
I don't have a scala compiler here so please forgive me for not trying,
but have you tried importing scala.Unit instead of import
scala.runtime.Unit? The scaladoc says to never use import
scala.runtime.Unit directly.
Just an idea.
Andreas
Thu, 2010-10-07, 16:17
#6
Re: What does my Java class need to implement so I can use it i
Hi Andreas,
scala.Unit does not exist as a class file.
ForScala/src$ javac -Xlint:unchecked -cp
$SCALA_HOME/lib/scala-library.jar forcomp/*.java
forcomp/CustomCollection.java:43: cannot find symbol
symbol : class Unit
location: package scala
public void foreach(Function1 b) {
^
1 error
Thanks anyway!
Daniel
On Thu, Oct 7, 2010 at 3:51 PM, Andreas Flierl wrote:
>
> Daniel Kröni wrote:
>> forcomp/Test.scala:9: error: type mismatch;
>> found : scala.Unit
>> required: scala.runtime.Unit
>> for(i <- cc){ println(""); }
>>
>> Any ideas what goes wrong here?
>
> I don't have a scala compiler here so please forgive me for not trying,
> but have you tried importing scala.Unit instead of import
> scala.runtime.Unit? The scaladoc says to never use import
> scala.runtime.Unit directly.
>
> Just an idea.
>
> Andreas
>
Thu, 2010-10-07, 17:17
#7
Re: What does my Java class need to implement so I can use it i
On Thu, Oct 07, 2010 at 03:24:26PM +0200, Daniel Kröni wrote:
> // for(i <- cc){ println("") } // DOES NOT WORK
It looks like a bug to me. It works this way though.
public void foreach(Function1 b) {
for (A a : wrapped) {
b.apply(a);
}
}
Thu, 2010-10-07, 17:37
#8
Re: What does my Java class need to implement so I can use it i
Haven't tried it, but what about scala.runtime.BoxedUnit? I don't program in java, but I think BoxedUnit wraps the primitive void. Or something like that.
On 2010-10-07 9:12 AM, "Daniel Kröni" <daniel.kroeni@gmail.com> wrote:> Hi Andreas,
> scala.Unit does not exist as a class file.
>
> ForScala/src$ javac -Xlint:unchecked -cp
> $SCALA_HOME/lib/scala-library.jar forcomp/*.java
> forcomp/CustomCollection.java:43: cannot find symbol
> symbol : class Unit
> location: package scala
> public void foreach(Function1<A, scala.Unit> b) {
> ^
> 1 error
>
> Thanks anyway!
> Daniel
>
>
> On Thu, Oct 7, 2010 at 3:51 PM, Andreas Flierl <andreas@flierl.eu> wrote:
>>
>> Daniel Kröni <daniel.kroeni@gmail.com> wrote:
>>> forcomp/Test.scala:9: error: type mismatch;
>>> found : scala.Unit
>>> required: scala.runtime.Unit
>>> for(i <- cc){ println(""); }
>>>
>>> Any ideas what goes wrong here?
>>
>> I don't have a scala compiler here so please forgive me for not trying,
>> but have you tried importing scala.Unit instead of import
>> scala.runtime.Unit? The scaladoc says to never use import
>> scala.runtime.Unit directly.
>>
>> Just an idea.
>>
>> Andreas
>>
Thu, 2010-10-07, 18:57
#9
Re: What does my Java class need to implement so I can use it i
On Thu, Oct 07, 2010 at 10:13:37AM -0600, Derek Williams wrote:
> Haven't tried it, but what about scala.runtime.BoxedUnit? I don't
> program in java, but I think BoxedUnit wraps the primitive void. Or
> something like that.
It's a reasonable theory, but BoxedUnit doesn't work either. I don't
think any concrete type will work, because it would have had to be
written with this in mind and using scala from java is not the common
case. I haven't looked too hard, but for now I don't think anything but
the generic signature I already posted will do the job.
Thu, 2010-10-07, 19:17
#10
Re: What does my Java class need to implement so I can use it i
I think you have to use the java.lang.Void class instead of scala.Unit. Scala will do its boxed primitive magic and treat it like Unit at runtime.
On Thu, Oct 7, 2010 at 7:51 PM, Paul Phillips <paulp@improving.org> wrote:
On Thu, Oct 7, 2010 at 7:51 PM, Paul Phillips <paulp@improving.org> wrote:
On Thu, Oct 07, 2010 at 10:13:37AM -0600, Derek Williams wrote:
> Haven't tried it, but what about scala.runtime.BoxedUnit? I don't
> program in java, but I think BoxedUnit wraps the primitive void. Or
> something like that.
It's a reasonable theory, but BoxedUnit doesn't work either. I don't
think any concrete type will work, because it would have had to be
written with this in mind and using scala from java is not the common
case. I haven't looked too hard, but for now I don't think anything but
the generic signature I already posted will do the job.
--
Paul Phillips | We act as though comfort and luxury were the chief
Apatheist | requirements of life, when all that we need to make us
Empiricist | really happy is something to be enthusiastic about.
all hip pupils! | -- Charles Kingsley
Thu, 2010-10-07, 19:37
#11
Re: What does my Java class need to implement so I can use it i
On Thu, Oct 07, 2010 at 08:12:35PM +0200, David Flemström wrote:
> I think you have to use the java.lang.Void class instead of
> scala.Unit. Scala will do its boxed primitive magic and treat it like
> Unit at runtime.
Another perfectly reasonable theory. As the only guy actually trying
things out I have to disappoint.
error: type mismatch;
found : Unit
required: java.lang.Void
for(i <- cc){ println("") } // DOES NOT WORK
^
one error found
Thu, 2010-10-07, 19:47
#12
Re: What does my Java class need to implement so I can use it i
If someone ported the Scala REPL to Android, I would be a very happy man.
On Thu, Oct 7, 2010 at 8:31 PM, Paul Phillips <paulp@improving.org> wrote:
On Thu, Oct 7, 2010 at 8:31 PM, Paul Phillips <paulp@improving.org> wrote:
On Thu, Oct 07, 2010 at 08:12:35PM +0200, David Flemström wrote:
> I think you have to use the java.lang.Void class instead of
> scala.Unit. Scala will do its boxed primitive magic and treat it like
> Unit at runtime.
Another perfectly reasonable theory. As the only guy actually trying
things out I have to disappoint.
error: type mismatch;
found : Unit
required: java.lang.Void
for(i <- cc){ println("") } // DOES NOT WORK
^
one error found
--
Paul Phillips | These are the climbs that apply men's soles.
Apatheist |
Empiricist |
i pull his palp! |----------* http://www.improving.org/paulp/ *----------
Thu, 2010-10-07, 19:57
#13
Re: What does my Java class need to implement so I can use it i
After thinking about it, and not having access to a dev machine to
test it, I would agree with you. If my method did work I have the
feeling that it would require an explicit Unit to be returned from the
function, where your method doesn't care what is returned. It is the
foreach method that has to return Unit (or void in Java).
On Thu, Oct 7, 2010 at 11:51 AM, Paul Phillips wrote:
> On Thu, Oct 07, 2010 at 10:13:37AM -0600, Derek Williams wrote:
>> Haven't tried it, but what about scala.runtime.BoxedUnit? I don't
>> program in java, but I think BoxedUnit wraps the primitive void. Or
>> something like that.
>
> It's a reasonable theory, but BoxedUnit doesn't work either. I don't
> think any concrete type will work, because it would have had to be
> written with this in mind and using scala from java is not the common
> case. I haven't looked too hard, but for now I don't think anything but
> the generic signature I already posted will do the job.
>
> --
> Paul Phillips | We act as though comfort and luxury were the chief
> Apatheist | requirements of life, when all that we need to make us
> Empiricist | really happy is something to be enthusiastic about.
> all hip pupils! | -- Charles Kingsley
>
Fri, 2010-10-08, 07:07
#14
Re: What does my Java class need to implement so I can use it i
On 08/10/2010, at 5:34 AM, David Flemström wrote:
> If someone ported the Scala REPL to Android, I would be a very happy man.
Yes, it would be useful sometimes. I think that would require one of:
a) making the scala compiler output dex,
b) porting the dx tool (which converts .class files to .dex files) to the phone, or
c) running a true JVM on the phone.
I don't know how tools such as 'J2ME MIDP Runner', , work (I think it might be a port of dx to the phone, but it could also use the net to offload that conversion to a server somewhere).
It looks like Esmertec Jbed is a full java ME vm that can run on some Android devices. Is there a port of the Scala REPL for javaME?
Be well,
Will :-}
Fri, 2010-10-08, 07:37
#15
Re: What does my Java class need to implement so I can use it i
Hi Paul,
I created a ticket #3908.
Thanks for your effort!
Daniel
On Thu, Oct 7, 2010 at 8:31 PM, Paul Phillips wrote:
> On Thu, Oct 07, 2010 at 08:12:35PM +0200, David Flemström wrote:
>> I think you have to use the java.lang.Void class instead of
>> scala.Unit. Scala will do its boxed primitive magic and treat it like
>> Unit at runtime.
>
> Another perfectly reasonable theory. As the only guy actually trying
> things out I have to disappoint.
>
> error: type mismatch;
> found : Unit
> required: java.lang.Void
> for(i <- cc){ println("") } // DOES NOT WORK
> ^
> one error found
>
> --
> Paul Phillips | These are the climbs that apply men's soles.
> Apatheist |
> Empiricist |
> i pull his palp! |----------* http://www.improving.org/paulp/ *----------
>
Fri, 2010-10-08, 14:17
#16
Re: What does my Java class need to implement so I can use it i
I'm not quite certain it is a bug, since the foreach method in Scala
requires a dummy return type as well, see:
http://www.scala-lang.org/api/current/scala/collection/Traversable.html
The use case for foreach has the function returning Unit, but the
actual implementation returns a type U, just a dummy type.
On Fri, Oct 8, 2010 at 12:34 AM, Daniel Kröni wrote:
> Hi Paul,
> I created a ticket #3908.
>
> Thanks for your effort!
> Daniel
>
>
> On Thu, Oct 7, 2010 at 8:31 PM, Paul Phillips wrote:
>> On Thu, Oct 07, 2010 at 08:12:35PM +0200, David Flemström wrote:
>>> I think you have to use the java.lang.Void class instead of
>>> scala.Unit. Scala will do its boxed primitive magic and treat it like
>>> Unit at runtime.
>>
>> Another perfectly reasonable theory. As the only guy actually trying
>> things out I have to disappoint.
>>
>> error: type mismatch;
>> found : Unit
>> required: java.lang.Void
>> for(i <- cc){ println("") } // DOES NOT WORK
>> ^
>> one error found
>>
>> --
>> Paul Phillips | These are the climbs that apply men's soles.
>> Apatheist |
>> Empiricist |
>> i pull his palp! |----------* http://www.improving.org/paulp/ *----------
>>
>
Fri, 2010-10-08, 16:27
#17
Re: What does my Java class need to implement so I can use it i
On Fri, Oct 08, 2010 at 07:16:13AM -0600, Derek Williams wrote:
> I'm not quite certain it is a bug, since the foreach method in Scala
> requires a dummy return type as well, see:
>
> http://www.scala-lang.org/api/current/scala/collection/Traversable.html
Socrates is a man, all men are mortal, but all men are not Socrates.
scala> object Bob { def foreach(f: Int => Unit): Unit = List(1, 2, 3) foreach f }
defined module Bob
scala> Bob foreach println
1
2
3
If you were to infer the necessary for comprehension signatures from
examining Traversable, you'd also think map requires a CanBuildFrom.
Fri, 2010-10-08, 16:57
#18
Re: What does my Java class need to implement so I can use it i
Does that not fail if the function returns something other than Unit? I think the specific issue I had was a runtime error when returning an AnyVal. A ClassCastException if I remember right. I will check as soon as I am able to.
On 2010-10-08 9:22 AM, "Paul Phillips" <paulp@improving.org> wrote:> On Fri, Oct 08, 2010 at 07:16:13AM -0600, Derek Williams wrote:
>> I'm not quite certain it is a bug, since the foreach method in Scala
>> requires a dummy return type as well, see:
>>
>> http://www.scala-lang.org/api/current/scala/collection/Traversable.html
>
> Socrates is a man, all men are mortal, but all men are not Socrates.
>
> scala> object Bob { def foreach(f: Int => Unit): Unit = List(1, 2, 3) foreach f }
> defined module Bob
>
> scala> Bob foreach println
> 1
> 2
> 3
>
> If you were to infer the necessary for comprehension signatures from
> examining Traversable, you'd also think map requires a CanBuildFrom.
>
> --
> Paul Phillips | Simplicity and elegance are unpopular because
> Analgesic | they require hard work and discipline to achieve
> Empiricist | and education to be appreciated.
> all hip pupils! | -- Dijkstra
Fri, 2010-10-08, 17:07
#19
Re: What does my Java class need to implement so I can use it i
On Fri, Oct 08, 2010 at 09:50:07AM -0600, Derek Williams wrote:
> Does that not fail if the function returns something other than Unit?
It's not relevant to the question at hand. The signature which doesn't
work in a java class returns "Unit" at least to the extent one can do so
from java in a way recognized by scala, which indeed is the whole issue.
Here it is once again:
On Thu, Oct 07, 2010 at 03:24:26PM +0200, Daniel Kröni wrote:
> // does NOT work as expected!
> public void foreach(Function1 b) {
> for (A a : wrapped) {
> b.apply(a);
> }
> }
Where your choices for nonworking return types for Function1 include at
least java.lang.Void, scala.runtime.BoxedUnit, and anything else which
seemed at all plausible.
However, if you like:
scala> object Obj { def foreach(x: Any => Any): Any = println("hi mom") }
defined module Obj
scala> for (x <- Obj) { x }
hi mom
res1: Any = ()
Fri, 2010-10-08, 17:27
#20
Re: What does my Java class need to implement so I can use it i
Ah, I see. The issue is no longer creating a foreach method in Java
that works with for comprehensions, but specifying a valid Function1
that returns Unit. My apologies.
On Fri, Oct 8, 2010 at 10:06 AM, Paul Phillips wrote:
>
> On Fri, Oct 08, 2010 at 09:50:07AM -0600, Derek Williams wrote:
>> Does that not fail if the function returns something other than Unit?
>
> It's not relevant to the question at hand. The signature which doesn't
> work in a java class returns "Unit" at least to the extent one can do so
> from java in a way recognized by scala, which indeed is the whole issue.
> Here it is once again:
>
> On Thu, Oct 07, 2010 at 03:24:26PM +0200, Daniel Kröni wrote:
>> // does NOT work as expected!
>> public void foreach(Function1 b) {
>> for (A a : wrapped) {
>> b.apply(a);
>> }
>> }
>
> Where your choices for nonworking return types for Function1 include at
> least java.lang.Void, scala.runtime.BoxedUnit, and anything else which
> seemed at all plausible.
>
> However, if you like:
>
> scala> object Obj { def foreach(x: Any => Any): Any = println("hi mom") }
> defined module Obj
>
> scala> for (x <- Obj) { x }
> hi mom
> res1: Any = ()
>
> --
> Paul Phillips | It's better to have gloved and tossed than never to
> Stickler | have played baseball.
> Empiricist |
> pull his pi pal! |----------* http://www.improving.org/paulp/ *----------
>
Fri, 2010-10-08, 19:07
#21
Re: What does my Java class need to implement so I can use it i
I can't even reproduce the error I was getting before so I must have
been doing something real nuts. That makes my point even less valid,
go me!
On Fri, Oct 8, 2010 at 10:06 AM, Paul Phillips wrote:
>
> On Fri, Oct 08, 2010 at 09:50:07AM -0600, Derek Williams wrote:
>> Does that not fail if the function returns something other than Unit?
>
> It's not relevant to the question at hand. The signature which doesn't
> work in a java class returns "Unit" at least to the extent one can do so
> from java in a way recognized by scala, which indeed is the whole issue.
> Here it is once again:
>
> On Thu, Oct 07, 2010 at 03:24:26PM +0200, Daniel Kröni wrote:
>> // does NOT work as expected!
>> public void foreach(Function1 b) {
>> for (A a : wrapped) {
>> b.apply(a);
>> }
>> }
>
> Where your choices for nonworking return types for Function1 include at
> least java.lang.Void, scala.runtime.BoxedUnit, and anything else which
> seemed at all plausible.
>
> However, if you like:
>
> scala> object Obj { def foreach(x: Any => Any): Any = println("hi mom") }
> defined module Obj
>
> scala> for (x <- Obj) { x }
> hi mom
> res1: Any = ()
>
> --
> Paul Phillips | It's better to have gloved and tossed than never to
> Stickler | have played baseball.
> Empiricist |
> pull his pi pal! |----------* http://www.improving.org/paulp/ *----------
>
My class isn't itself an ArrayList. It's an ordinary Java class that extends
Object.
It internally references an ArrayList, whose elements I want to expose
sufficiently
for my class to be a valid generator for the Scala for-comprehension.
(To preserve encapsulation, I don't want to expose the ArrayList directly.)
My suspicion is that it needs to implement an Iterator.
I was expecting that all the work would need to be in the Java code, and not
in the Scala code.
Does that help clarify the requirement?