Hi John,

Thanks for investigating rather than taking such claims on face
value.  Indeed, the Scala language is compatible with Java's List
hierarchy.  As you point out, scala.List is just another class which
you can use or not use.

There is one caveat that brings a small kernel of truth to the
original claim.  Scala had something called for comprehensions well
before Java had its enhanced for loop.  Scala's for comprehension is a
wicked powerful construct - think of Python's list comprehensions
generalized.  But in order to use for comprehensions on Java lists,
you have to import some conversion routines.  For this purpose, you
can think of these conversion routines as being very similar to the
way C# lets you add "extension methods" or as a safe, static,
lexically scoped way to do what Ruby and Python programmers call
"monkey patching".  It's not monkey patching since you aren't actually
modifying the object in any way, and you get to control the scope of
the conversion, but it's a useful way to think about it.  In this
case, we need to add a method called "foreach" (similar to "each" in
Ruby).

scala> import java.util.List
import java.util.List

scala> import java.util.ArrayList
import java.util.ArrayList

scala> import scala.collection.jcl.Conversions._
import scala.collection.jcl.Conversions._

scala> val names : List[String] = new ArrayList[String]
names: java.util.List[String] = []

scala> names add "bob"
res0: Boolean = true

scala> names add "chuck"
res1: Boolean = true

scala> for (name <- names) println(name)
bob
chuck

I've removed some of the unneeded syntactic noise like semicolons, but
this starts as basically the same code as yours plus one extra import
at the top.  That one extra import allows me to write the for code at
the bottom.  To begin to see what's nice about Scala's "for", look at
this

scala> val upperNames = for (name <- names) yield name.toUpperCase

This creates a new list of upper cased names in one easy, and easy to
read, line.


On Dec 23, 4:19 pm, John Debeard <[email protected]> wrote:
> I'm really new to Scala so this might not be really good.   Riener
> made me wonder if Scala was incompatible with Java Lists so I had to
> try to see if he was right.
>
> scala> import java.util.List;
> import java.util.List
>
> scala> import java.util.ArrayList;
> import java.util.ArrayList
>
> scala> val names : List[String] = new ArrayList[String]();
> names: java.util.List[String] = []
>
> scala> names.add("bob");
> res0: Boolean = true
>
> scala> names.add("chuck");
> res1: Boolean = true
>
> scala> names.contains("bob");
> res2: Boolean = true
>
> It worked for me.  I know Scala has another List in scala.List but I
> know Java has another List in java.util.List and in java.awt.List.
> So I don't understand what your saying.
>
> On Dec 23, 2:45 pm, Reinier Zwitserloot <[email protected]> wrote:
>
> > Scala's Lists aren't compatible with java's lists. Scala needs 'views'
> > because without them things aren't properly compatible. Your own say
> > so:
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to