Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Their role is a bit similar to the ones (i.e. multiple type parameter) in a class, since traits are, after all, classes (without any constructor) meant to be added to some other class as a mixin.</p> <p>The <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf" rel="nofollow">Scala spec</a> gives the following example for Trait with multiple parameters:</p> <blockquote> <p>Consider an abstract class Table that implements maps from a type of keys A to a type of values B.<br> The class has a method set to enter a new key/value pair into the table, and a method get that returns an optional value matching a given key.<br> Finally, there is a method apply which is like get, except that it returns a given default value if the table is undefined for the given key. This class is implemented as follows.</p> </blockquote> <pre><code>abstract class Table[A, B](defaultValue: B) { def get(key: A): Option[B] def set(key: A, value: B) def apply(key: A) = get(key) match { case Some(value) =&gt; value case None =&gt; defaultValue } } </code></pre> <blockquote> <p>Here is a concrete implementation of the Table class.</p> </blockquote> <pre><code>class ListTable[A, B](defaultValue: B) extends Table[A, B](defaultValue) { private var elems: List[(A, B)] def get(key: A) = elems.find(._1.==(key)).map(._2) def set(key: A, value: B) = { elems = (key, value) :: elems } } </code></pre> <blockquote> <p>Here is a trait that prevents concurrent access to the get and set operations of its parent class</p> </blockquote> <pre><code>trait Synchronized Table[A, B] extends Table[A, B] { abstract override def get(key: A): B = synchronized { super.get(key) } abstract override def set((key: A, value: B) = synchronized { super.set(key, value) } } </code></pre> <blockquote> <p>Note that <code>SynchronizedTable</code> does not pass an argument to its superclass, <code>Table</code>, even though <code>Table</code> is defined with a formal parameter.<br> Note also that the super calls in <code>SynchronizedTable</code>’s get and set methods statically refer to abstract methods in class <code>Table</code>. This is legal, as long as the calling method is labeled abstract override (§5.2).</p> <p>Finally, the following mixin composition creates a synchronized list table with strings as keys and integers as values and with a default value 0:</p> </blockquote> <pre><code>object MyTable extends ListTable[String, Int](0) with SynchronizedTable </code></pre> <blockquote> <p>The object <code>MyTable</code> inherits its get and set method from <code>SynchronizedTable</code>.<br> The super calls in these methods are re-bound to refer to the corresponding implementations in <code>ListTable</code>, which is the actual supertype of <code>SynchronizedTable</code> in <code>MyTable</code>.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload