Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between a "trait" and a "template trait"?

Tags:

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!

like image 700
Adam Avatar asked Jan 05 '11 00:01

Adam


People also ask

What is trait in Java?

A trait is kind of a “micro interface” that describes some characteristic of a class design that can be found in many different components throughout the system. By referring to the traits instead of the implementing class itself you can keep the system decoupled and modular.

What is a trait class?

Definition of Class Trait (noun) A behavior, custom, or norm–either real or imagined–that define or reflect a class.


2 Answers

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

like image 149
Aaron Novstrup Avatar answered Oct 14 '22 17:10

Aaron Novstrup


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

like image 28
IttayD Avatar answered Oct 14 '22 18:10

IttayD