I'm looking at Scala Collections implementation in 2.12 and have some question about the design. There are some "abstractions types" used in there:
$Coll$Like
Gen$Coll$Like
Gen$Coll$
As far as I understand the Gen$Coll$Like traits exist solely for purpose of reusing declarations (not definitions), i.e. all methods in Gen$Coll$Like traits are abstract.
$Coll$Like in turn declares a few abstract methods and implement the rest of the Gen$Coll$Like methods through them. Besides that $Coll$Like-traits have additional type parameter +Repr which I believe is for cases like SeqLike[Char, String] so we can look at String as Seq[Char].
The most questionable traits are Gen$Coll$. I do not really understand the point of them. In the hierarchy of Seq all Gen$Coll$ traits are empty till GenTraversableOnce (also, I expected GenTraversableOnce to be named GenTraversableOnceLike since it provides abstract method declarations only):
GenSeq <--- GenIterable <--- GenTraversable <--- GenTraversableOnce
Can you explain the purpose of Gen$Coll$ traits? The example of where
Gen$Coll$ actually useful (for any purpose) would be highly appreciate.
Related
In OOP it is good practice to talk to interfaces not to implementations. So, e.g., you write something like this (by Seq I mean scala.collection.immutable.Seq :)):
// talk to the interface - good OOP practice
doSomething[A](xs: Seq[A]) = ???
not something like the following:
// talk to the implementation - bad OOP practice
doSomething[A](xs: List[A]) = ???
However, in pure functional programming languages, such as Haskell, you don't have subtype polymorphism and use, instead, ad hoc polymorphism through type classes. So, for example, you have the list data type and a monadic instance for list. You don't need to worry about using an interface/abstract class because you don't have such a concept.
In hybrid languages, such as Scala, you have both type classes (through a pattern, actually, and not first-class citizens as in Haskell, but I digress) and subtype polymorphism. In scalaz, cats and so on you have monadic instances for concrete types, not for the abstract ones, of course.
Finally the question: given this hybridism of Scala do you still respect the OOP rule to talk to interfaces or just talk to concrete types to take advantage of functors, monads and so on directly without having to convert to a concrete type whenever you need to use them? Put differently, is in Scala still good practice to talk to interfaces even if you want to embrace FP instead of OOP? If not, what if you chose to use List and, later on, you realized that a Vector would have been a better choice?
P.S.: In my examples I used a simple method, but the same reasoning applies to user defined types. E.g.:
case class Foo(bars: Seq[Bar], ...)
What I would attack here is your "concrete vs. interface" concept. Look at it this way: every type has an interface, in the general sense of the term "interface." A "concrete" type is just a limiting case.
So let's look at Haskell lists from this angle. What's the interface of a list? Well, lists are an algebraic data type, and all such data types have the same general form of interface and contract:
You can construct instances of the type using its constructors according to their arities and argument types;
You can observe instances of the type by matching against their constructors according to their arities and argument types;
Construction and observation are inverses—when you pattern match against a value, what you get out is exactly what was put into it.
If you look at it in these terms, I think the following rule works pretty well in either paradigm:
Choose types whose interfaces and contracts match exactly with your requirements.
If their contract is weaker than your requirements, then they won't maintain invariants that you need;
If their contracts are stronger than your requirements, you may unintentionally couple yourself to the "extra" details and limit your ability to change the program later on.
So you no longer ask whether a type is "concrete" or "abstract"—just whether it fits your requirements.
These are my two cents on this subject. In Haskell you have data types (ADTs). You have both lists (linked lists) and vectors (int-indexed arrays) but they don't share a common supertype. If your function takes a list you cannot pass it a vector.
In Scala, being it a hybrid OOP-FP language, you have subtype polymorphism too so you may not care if the client code passes a List or a Vector, just require a Seq (possibly immutable) and you're done.
I guess to answer to this question you have to ask yourself another question: "Do I want to embrace FP in toto?". If the answer is yes then you shouldn't use Seq or any other abstract superclass in the OOP sense. Of course, the exception to this rule is the use of a trait/abstract class when defining ADTs in Scala. For example:
sealed trait Tree[+A]
case object Empty extends Tree[Nothing]
case class Node[A](value: A, left: Tree[A], right: Tree[A]) extends Tree[A]
In this case one would require Tree[A] as a type, of course, and then use, e.g., pattern matching to determine if it's either Empty or Node[A].
I guess my feeling about this subject is confirmed by the red book (Functional Programming in Scala). There they never use Seq, but List, Vector and so on. Also, haskellers, don't care about these problems and use lists whenever they need linked-list semantic and vectors whenever they need int-indexed-array semantic.
If, on the other hand, you want to embrace OOP and use Scala as a better Java then OK, you should follow the OOP best practice to talk to interfaces not to implementations.
If you're thinking: "I'd rather opt for mostly functional" then you should read Erik Meijer's The Curse of the Excluded Middle.
I'm new to Scala, coming from Java, and I was just reading about traits. One thing that gets mentioned often is that traits don't (can't? won't?) have constructor parameters. I was curious to know if there was a reason for this.
Coming from a long ago maths/computer-science background I was was wondering if this was an inevitable consequence because of some language design decision, or if it was a conscious decision to avoid some inheritance/mix-in problem or another?
Was hoping someone might know because it feels like there might be something interesting behind the fact.
The other answers describe the language; I suspect your question may really be "why is it designed in this way".
I believe it arises out of the awkwardnesses and verboseness that would arise when extending multiple traits, especially with overrides and with types, and various mix-in strategies.
The Cake Pattern often results in various traits providing missing bits to each other in a way that is totally invisible - by design - in the mixing class. And mixing can be bi-directional, using self-types. So the construction of a class from traits can be a very messy business for the compiler. Scala often trades simplicity of compiler design and implementation for simplicity of language use and code reduction, and this is certainly a good example.
So while there may be simple, hierarchical cases where having a constructor might be useful and sufficient, it would almost certainly have to be redundant of other mechanisms for more difficult, non-hierarchical scenarios.
Scala 3 will allow trait parameters. Here's a sample from the docs
trait Greeting(val name: String) {
def msg = s"How are you, $name"
}
class C extends Greeting("Bob") {
println(msg)
}
The answer is: that's what Scala is right now.
But that might not be the case in the future: trait parameters can replace early initializers. (see Martin Odersky's recent Scala Days presentation page 34)
Scala: Where It Came From & Where It is Going
Traits don't have constructor parameters because traits cannot be constructed. Given any trait T it's not possible to instantiate any object of type exactly T. You can override trait defs with vals though, so
trait Foo {
def bar: String
}
class Baz(override val bar: String) extends Foo
You can't construct them directly because new MyTrait {} is actually sugar for an anonymous class of new Object with MyTrait {}
Trait is analog for Java Interface. The main difference is that trait can have default implementation for their methods.
So Java interfaces can't have constructor so do Scala traits
Scala 3 allows traits with parameters, just like classes have parameters.
I'm new to scala(just start learning it), but have figured out smth strange for me: there are classes Array and List, they both have such methods/functions as foreach, forall, map etc. But any of these methods aren't inherited from some special class(trait). From java perspective if Array and List provide some contract, that contract have to be declared in interface and partially implemented in abstract classes. Why do in scala each type(Array and List) declares own set of methods? Why do not they have some common type?
But any of these methods aren't inherited from some special class(trait)
That simply not true.
If you open scaladoc and lookup say .map method of Array and List and then click on it you'll see where it is defined:
For list:
For array:
See also info about Traversable and Iterable both of which define most of the contracts in scala collections (but some collections may re-implement methods defined in Traversable/Iterable, e.g. for efficiency).
You may also want to look at relations between collections (scroll to the two diagrams) in general.
I'll extend om-nom-nom answer here.
Scala doesn't have an Array -- that's Java Array, and Java Array doesn't implement any interface. In fact, it isn't even a proper class, if I'm not mistaken, and it certainly is implemented through special mechanisms at the bytecode level.
On Scala, however, everything is a class -- an Int (Java's int) is a class, and so is Array. But in these cases, where the actual class comes from Java, Scala is limited by the type hierarchy provided by Java.
Now, going back to foreach, map, etc, they are not methods present in Java. However, Scala allows one to add implicit conversions from one class to another, and, through that mechanism, add methods. When you call arr.foreach(println), what is really done is Predef.refArrayOps(arr).foreach(println), which means foreach belongs to the ArrayOps class -- as you can see in the scaladoc documentation.
Could someone please help me understand Scala's various "Like" traits in the collection API. I've been reading over and trying to compare each without luck. I think I can see that Map for example, extends MapLike - adding 2 concrete methods. But this begs the question of why do this at all? Why not just have 1 Map trait in the Collections API instead of Map and MapLike?
Thank you!
The best source for these details is Martin Odersky and Lex Spoon's "What's New in Scala 2.8: The Architecture of Scala Collections":
The Scala collection library avoids code duplication and achieves the
"same-result-type" principle by using generic builders and traversals
over collections in so-called implementation traits. These traits
are named with a Like suffix; for instance, IndexedSeqLike is the
implementation trait for IndexedSeq, and similarly,
TraversableLike is the implementation trait for Traversable.
Collection classes such as Traversable or IndexedSeq inherit all
their concrete method implementations from these traits.
Implementation traits have two type parameters instead of one for
normal collections. They parameterize not only over the collection's
element type, but also over the collection's representation type,
i.e., the type of the underlying collection, such as Seq[I] or List[T]...
The whole article is extremely useful if you want to integrate your own collection classes with the Collections API, or if you just want a deeper understanding of how the library works.
Looking at the scaladoc for Traversable and TraversableLike, I'm having a hard time figuring out what the difference between them is (except that one extends the other). The only apparent difference in the documentation is that it says Traversable is a "trait" and TraversableLike is a "template trait". But googling for "template trait" does not reveal a definition for this term. Help!
I haven't seen this terminology in general use in Scala, and I think it's specific to the design of the Scala collections API. You can learn more by reading The Architecture of Scala Collections (especially the section on "factoring out common operations")[1] and the Scala collections SID. §4.2 of the SID is relevant, although they're referred to as "implementation traits" there:
Collection classes such as Traversable or Vector inherit all their concrete method implementations from an implementation trait. These traits are named with the Like suffix; for instance VectorLike is the implementation trait for Vector and TraversableLike is the implementation trait for Traversable.
In short, their purpose is both to separate implementation for use outside the collections hierarchy (e.g. StringOps extends TraversableLike but not Traversable) and to factor out common operations in such a way that the collection type is preserved (see IttayD's answer for a more thorough explanation).
I should note that you really don't need to be concerned with these classes unless you are extending the collections hierarchy. For ordinary use, focus on the Traversable, Iterable, Seq, etc. traits. If you're new to the Scala Collections API, I would suggest starting with the Scala 2.8 Collection API document, then reference the scaladoc as necessary. You can't expect to get the 'big picture' looking through the scaladoc.
[1] credit goes to michid for this link
The XXXLike traits have an important role of adding the Repr generic parameter. Methods that are supposed to return the same collection type, like filter, map, flatMap, are implemented in low level traits (TraversableLike). To encode their return type, those traits receive it:
trait TraversableLike[+A, +Repr] ...
...
def filter(p: A => Boolean): Repr = {
(for map and flatMap the issue is more complicated, I won't go into it here)
Now say you have a new type of collection. You could do:
trait MyCollection[+A] extends TraversableLike[A, MyCollection]
But then if anyone wants to extend your collection, they are stuck with return values of MyCollection from the various inherited methods.
So instead, you create:
trait MyCollectionLike[+A, +Repr] extends TraversableLike[A, Repr]
and
trait MyCollection[+A] extends MyCollectionLike[A, MyCollection]
and anyone that wants to extend your collection extends MyCollectionLike
The [...]Like classes are implementation classes for the actual collection classes. In a sense they act like template implementations from which most - if not all - behavior is inherited by the actual collection classes.
For a very detailed and approachable overview read The Architecture of Scala Collections.