logo       

Re: Another nit: msg#00511

lang.scala

Subject: Re: Another nit

Tony Morris <tmorris@xxxxxxxxxxx> writes:
> > By existing APIs, do you include scala.Stream, by the way? The name
> > scala.Stream makes it hard to find for people who are looking for
> > "Lazy List".
>
> Yes, I have seen scala.Stream, but it took Martin to point it out to me
> for the reasons that you mention. I don't like the fact that its type
> parameter is invariant, nor the fact that it is a trait.

Actually, it is covariant. We need to fix the API docs to make this
clearer. Notice, for example, that "Stream.empty" is actually a
singleton value of type Stream[Nothing]. This works due to the
covariance.

On the other question, why does it matter if it's a trait?


> >> MyList[A] <-> scala.List[A]
> >> MyList[A] <-> scala.Stream[A]
> >> MyList[A] <-> scala.Iterator[A]
> >
> > These are generally cool, but this third one looks weird to me. Scala
> > carefully distinguishes Iterable's from Iterator's. If you want an
> > iterator, you send elements.
>
> I don't understand your point here; an Iterator *is* a lazy list, just
> in the imperative model - I had to write imperative code with a
> destructive update (cough, choke) in that method. I'm interested in
> understanding your point here.

My point is that in Scala, you can access a collection both ways:
destructive once-only iteration (Iterator), and non-destructively
(Iterable). You start with an Iterable, and if you want the
destructive mode then you explicitly call "elements".

Maybe the library should be arranged differently, but that is how
things are set up right now.



> >> But then you can get really funky within a mixin. How about the ordering
> >> relationship?
> >> trait Ord[A] extends Equal[A] {
> >
> > This one is in the library, by the way. Great minds think alike. :)
>
> Where is it?

scala.Ordered . You define a compare method and then get the other
orderings automatically (<, <=, >, >=).

def compare (that: a): Int


You do not get equals(), because the trait does not know how to define
hashCode(). If you use this, you'll probably also want to override
hashCode() and equals().


-Lex




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise