Comments on Programming Scalaurn:uuid:8cda2744-bc83-309c-a18a-d12b4e8de0bb2009-08-11T20:58:42-07:00 <div>Linearization of an Object’s Hierarchy <p id="para_all_our_classes_and_traits_def">All our classes and traits define the method <code class="literal">m</code>. The one in <code class="literal">C3</code> is called first, since the instance is of that type. <code class="literal">C3.m</code> calls <code class="literal">super.m</code>, which resolves to <code class="literal">T3.m</code>. The search appears to be &#8220;breadth-first&#8221;, rather than &#8220;depth-first&#8221;. (If it were depth-first, it would invoke <code class="literal">C1.m</code> after <code class="literal">T3.m</code>.) After, <code class="literal">T3.m</code>, <code class="literal">T2.m</code>, then <code class="literal">T1.m</code>, and finally <code class="literal">C1.m</code> are invoked. <code class="literal">C1</code> is the parent of the three traits. From which of the traits did we traverse to <code class="literal">C1</code>? Let&#8217;s modify our first example and see how we got to <code class="literal">C1</code>.</p> <blockquote><p>Saying it's breadth-first is misleading, IMHO. It is actually depth first, with a twist: it leaves off handling each node for as late as possible. So, you could say that it <em>does</em> examine C1 after T3, but does not handle it, since it will have the opportunity to visit it from T1 later. </p> <p>The distinction is very clear if you insert an additional layer of traits, T1B, T2B and T3B, between T1-3 and C2 (meaning, T1B extends T1, etc., and C2 extends T1B with T2B with T3B. Then the order of invocation will be C2, T3B, T3, T2B, T2, T1B, T1, C1. So, basically it's depth first, with the modification as noted above. </p> <p>I know you specify the full algorithm below, but you never repudiate the "breadth-first" claim, and some people may be left with a misconception. </p> &#8212; glebfrank (2009-07-17 09:41:07) </blockquote> </div>urn:uuid:d61f9568-eace-3a64-b1e9-c66008318e72glebfrank2009-07-17T09:41:07-07:00Comment on 'Linearization of an Object’s Hierarchy' <div>Classes and Objects: Where Are the Statics? <p id="para_another_benefit_of_package_obj">Another benefit of package objects is that it provides a more succinct implementation of what was an awkward idiom before. Without package objects, you would have to put definitions in an <span class="emphasis"><em>ad hoc</em></span> object inside the desired package, then import from the object. For example, here is how <code class="literal">List</code> would have to be handled without a package object.</p> <blockquote><p>Can you explain how this awkward idiom is needed? </p> &#8212; ctran (2009-07-12 21:01:51) </blockquote> </div>urn:uuid:5aba2c7c-7b65-3c6b-944c-ee879cae97cbctran2009-07-12T21:01:51-07:00Comment on 'Classes and Objects: Where Are the Statics?' <div>Linearization of an Object’s Hierarchy <p id="para_scala_inserts_the_scalaobject">Scala inserts the <code class="literal">ScalaObject</code> trait as the last mixin, just before <code class="literal">AnyRef</code> and <code class="literal">Any</code> that are the penultimate and ultimate parent classes of any reference type. Of course, these three types do not show up in the output of the scripts, because we used an <span class="emphasis"><em>ad hoc</em></span> <code class="literal">m</code> method to figure out the behavior by building up an output string.</p> <blockquote><p>Thx. </p> &#8212; deanwampler (2009-07-05 06:58:15) </blockquote> </div>urn:uuid:4750a85f-f460-3074-80ca-78e0da13ad08deanwampler2009-07-05T06:58:15-07:00Comment on 'Linearization of an Object’s Hierarchy' <div>The Scala Type Hierarchy <div class="table" id="AnyRef-reference-types"><p class="title"><b>Table&#160;7.4.&#160;Direct and indirect subtypes of <code class="literal">AnyRef</code>, the <span class="emphasis"><em>reference types</em></span>.</b></p><div class="table-contents"><table summary="Direct and indirect subtypes of AnyRef, the reference types." border="0"><colgroup><col align="left" /><col align="left" /><col align="left" /></colgroup><thead valign="top"><tr><th align="left" valign="top"> Name </th><th align="left" valign="top"> Parent </th><th align="left" valign="top"> Description</th></tr></thead><tbody valign="top"><tr><td align="left" valign="top"><p><code class="literal">Collection[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Iterable[T]</code></p></td><td align="left" valign="top"><p>Trait for collections of known size.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Either[+T1, +T2]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Used most often as a return type when a method could return an instance of one of two unrelated types. For example, an exception or a &#8220;successful&#8221; result. The <code class="literal">Either</code> can be pattern matched for its <code class="literal">Left</code> or <code class="literal">Right</code> subtypes. (It is analogous to <code class="literal">Option</code>, with <code class="literal">Some</code> and <code class="literal">None</code>) For the exception-handling idiom, it is conventional to use <code class="literal">Left</code> for the exception.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">FunctionN[-T</code><sub>1</sub><code class="literal">, -T</code><sub>2</sub><code class="literal">, &#8230;, -T</code><sub>N</sub><code class="literal">, +R]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait representing a function that takes <code class="literal">N</code> arguments, each of which can have its own type, and returns a value of type <code class="literal">R</code>. (Traits are defined for <code class="literal">N</code> = 0 to 22.) The <span class="emphasis"><em>variance annotations</em></span> (&#8216;+&#8217; and &#8216;-&#8217;) in front of the types will be explained in <a class="xref" href="ch12.html#VarianceUnderInheritance" title="Variance Under Inheritance">the section called &#8220;Variance Under Inheritance&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Iterable[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait with methods for operating on collections of instances. Users implement the abstract <code class="literal">elements</code> method to return an <code class="literal">Iterable</code> instance.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">List[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Seq[T]</code></p></td><td align="left" valign="top"><p><code class="literal">sealed abstract class</code> for ordered collections with functional-style list semantics. It is the most widely-used collection in Scala, so it is defined in the <code class="literal">scala</code> package, rather than one of the collection packages. (In Scala version2.8, it is actually defined in <code class="literal">scala.collection.immutable</code> and &#8220;aliased&#8221; in <code class="literal">package object scala</code>). It has two subclasses, <code class="literal">case object Nil</code>, which extends <code class="literal">List[Nothing]</code> and represents an empty list, and <code class="literal">case final class ::[T]</code>, which represents a non-empty list, characterized by a head element and a tail list, which would be <code class="literal">Nil</code> for a one-element list.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Nothing</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>all other types</em></span></p></td><td align="left" valign="top"><p><code class="literal">Nothing</code> is the subtype of <span class="emphasis"><em>all</em></span> other types. It has no instances. It is used primarily for defining other types in a type-safe way, such as the special <code class="literal">List</code> subtype <code class="literal">Nil</code>. See also <a class="xref" href="ch12.html#NothingAndNull" title="Nothing and Null">the section called &#8220;Nothing and Null&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Null</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>all reference types</em></span></p></td><td align="left" valign="top"><p><code class="literal">Null</code> has one instance, <code class="literal">null</code>, corresponding to the runtime&#8217;s concept of <code class="literal">null</code>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Option[T]</code></p></td><td align="left" valign="top"><p><code class="literal">Product</code></p></td><td align="left" valign="top"><p>Wraps an optional item. It is a <code class="literal">sealed abstract</code> type and the only allowed instances are an instance of its derived <code class="literal">case class Some[T]</code>, wrapping an instance of <code class="literal">T</code>, or its derived <code class="literal">case object None</code>, which extends <code class="literal">Option[Nothing]</code>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Predef</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>An <code class="literal">object</code> that defines and imports many commonly-used types and methods. See <a class="xref" href="ch07.html#PredefObject" title="The Predef Object">the section called &#8220;The Predef Object&#8221;</a> earlier in this chapter for details.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Product</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait with methods for determining arity and getting the n<sup>th</sup> item in a &#8220;cartesian product&#8221;. Subtraits are defined for <code class="literal">Product</code>, called <code class="literal">ProductN</code>, for dimension <code class="literal">N</code> from 1 through 22.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">ScalaObject</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>Mixin</em></span> trait added to all Scala reference type instances.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Seq[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Collection[T]</code></p></td><td align="left" valign="top"><p>Trait for ordered collections.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">TupleN</code></p></td><td align="left" valign="top"><p><code class="literal">ProductN</code></p></td><td align="left" valign="top"><p>Separate case classes for arity <code class="literal">N</code> = 1 through 22. Tuples support the <span class="emphasis"><em>literal</em></span> syntax <code class="literal">(x1, x2, &#8230;, xN)</code>.</p></td></tr></tbody></table></div></div> <blockquote><p>Fixed. Thx. </p> &#8212; deanwampler (2009-07-05 06:51:48) </blockquote> </div>urn:uuid:d0275a85-d4ec-3a7a-82b5-b340451ebe1ddeanwampler2009-07-05T06:51:48-07:00Comment on 'The Scala Type Hierarchy' <div>Sealed Class Hierarchies <p id="para_output3">This script outputs the following.</p> <blockquote><p>Doh! Copy &amp; paste ... </p> &#8212; deanwampler (2009-07-05 06:47:06) </blockquote> </div>urn:uuid:326053d8-38a0-3005-8864-9b2d40278cb7deanwampler2009-07-05T06:47:06-07:00Comment on 'Sealed Class Hierarchies' <div>The Predef Object <p id="para_suppose_we_declare_a_var_of_">Briefly, a <code class="literal">Pair[A2,B2]</code>, for some <code class="literal">A2</code> and <code class="literal">B2</code>, is a <span class="emphasis"><em>subclass</em></span> of <code class="literal">Pair[A1,B1]</code>, for some <code class="literal">A1</code> and <code class="literal">B1</code>, if <code class="literal">A2</code> is a subtype of <code class="literal">A1</code> and <code class="literal">B2</code> is a subtype of <code class="literal">B1</code>. In <a class="xref" href="ch12.html#ParameterizedTypes" title="Understanding Parameterized Types">the section called &#8220;Understanding Parameterized Types&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>, we&#8217;ll discuss &#8216;+&#8217; and other type qualifiers in more detail.</p> <blockquote><p>Thanks. Will fix </p> &#8212; deanwampler (2009-07-05 06:34:01) </blockquote> </div>urn:uuid:e4906ce2-6d11-3c7d-8e37-e49c2cf16472deanwampler2009-07-05T06:34:01-07:00Comment on 'The Predef Object' <div>Linearization of an Object’s Hierarchy <p id="para_scala_inserts_the_scalaobject">Scala inserts the <code class="literal">ScalaObject</code> trait as the last mixin, just before <code class="literal">AnyRef</code> and <code class="literal">Any</code> that are the penultimate and ultimate parent classes of any reference type. Of course, these three types do not show up in the output of the scripts, because we used an <span class="emphasis"><em>ad hoc</em></span> <code class="literal">m</code> method to figure out the behavior by building up an output string.</p> <blockquote><p>we used an our &lt;- ad hoc m method </p> &#8212; robcd (2009-06-22 00:16:03) </blockquote> </div>urn:uuid:4c3dc050-2a1e-3ab8-9904-794d86455917robcd2009-06-22T00:16:03-07:00Comment on 'Linearization of an Object’s Hierarchy' <div>Sealed Class Hierarchies <p id="para_output3">This script outputs the following.</p> <blockquote><p>only need 'case Connect(body) =&gt;' in def handle, in Ex 7.7, 7.8 </p> &#8212; robcd (2009-06-21 23:59:12) </blockquote> </div>urn:uuid:ce74d13f-165f-30c1-b15b-99cb144f4929robcd2009-06-21T23:59:12-07:00Comment on 'Sealed Class Hierarchies' <div>The Scala Type Hierarchy <div class="table" id="AnyRef-reference-types"><p class="title"><b>Table&#160;7.4.&#160;Direct and indirect subtypes of <code class="literal">AnyRef</code>, the <span class="emphasis"><em>reference types</em></span>.</b></p><div class="table-contents"><table summary="Direct and indirect subtypes of AnyRef, the reference types." border="0"><colgroup><col align="left" /><col align="left" /><col align="left" /></colgroup><thead valign="top"><tr><th align="left" valign="top"> Name </th><th align="left" valign="top"> Parent </th><th align="left" valign="top"> Description</th></tr></thead><tbody valign="top"><tr><td align="left" valign="top"><p><code class="literal">Collection[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Iterable[T]</code></p></td><td align="left" valign="top"><p>Trait for collections of known size.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Either[+T1, +T2]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Used most often as a return type when a method could return an instance of one of two unrelated types. For example, an exception or a &#8220;successful&#8221; result. The <code class="literal">Either</code> can be pattern matched for its <code class="literal">Left</code> or <code class="literal">Right</code> subtypes. (It is analogous to <code class="literal">Option</code>, with <code class="literal">Some</code> and <code class="literal">None</code>) For the exception-handling idiom, it is conventional to use <code class="literal">Left</code> for the exception.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">FunctionN[-T</code><sub>1</sub><code class="literal">, -T</code><sub>2</sub><code class="literal">, &#8230;, -T</code><sub>N</sub><code class="literal">, +R]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait representing a function that takes <code class="literal">N</code> arguments, each of which can have its own type, and returns a value of type <code class="literal">R</code>. (Traits are defined for <code class="literal">N</code> = 0 to 22.) The <span class="emphasis"><em>variance annotations</em></span> (&#8216;+&#8217; and &#8216;-&#8217;) in front of the types will be explained in <a class="xref" href="ch12.html#VarianceUnderInheritance" title="Variance Under Inheritance">the section called &#8220;Variance Under Inheritance&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Iterable[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait with methods for operating on collections of instances. Users implement the abstract <code class="literal">elements</code> method to return an <code class="literal">Iterable</code> instance.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">List[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Seq[T]</code></p></td><td align="left" valign="top"><p><code class="literal">sealed abstract class</code> for ordered collections with functional-style list semantics. It is the most widely-used collection in Scala, so it is defined in the <code class="literal">scala</code> package, rather than one of the collection packages. (In Scala version2.8, it is actually defined in <code class="literal">scala.collection.immutable</code> and &#8220;aliased&#8221; in <code class="literal">package object scala</code>). It has two subclasses, <code class="literal">case object Nil</code>, which extends <code class="literal">List[Nothing]</code> and represents an empty list, and <code class="literal">case final class ::[T]</code>, which represents a non-empty list, characterized by a head element and a tail list, which would be <code class="literal">Nil</code> for a one-element list.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Nothing</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>all other types</em></span></p></td><td align="left" valign="top"><p><code class="literal">Nothing</code> is the subtype of <span class="emphasis"><em>all</em></span> other types. It has no instances. It is used primarily for defining other types in a type-safe way, such as the special <code class="literal">List</code> subtype <code class="literal">Nil</code>. See also <a class="xref" href="ch12.html#NothingAndNull" title="Nothing and Null">the section called &#8220;Nothing and Null&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Null</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>all reference types</em></span></p></td><td align="left" valign="top"><p><code class="literal">Null</code> has one instance, <code class="literal">null</code>, corresponding to the runtime&#8217;s concept of <code class="literal">null</code>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Option[T]</code></p></td><td align="left" valign="top"><p><code class="literal">Product</code></p></td><td align="left" valign="top"><p>Wraps an optional item. It is a <code class="literal">sealed abstract</code> type and the only allowed instances are an instance of its derived <code class="literal">case class Some[T]</code>, wrapping an instance of <code class="literal">T</code>, or its derived <code class="literal">case object None</code>, which extends <code class="literal">Option[Nothing]</code>.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Predef</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>An <code class="literal">object</code> that defines and imports many commonly-used types and methods. See <a class="xref" href="ch07.html#PredefObject" title="The Predef Object">the section called &#8220;The Predef Object&#8221;</a> earlier in this chapter for details.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Product</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p>Trait with methods for determining arity and getting the n<sup>th</sup> item in a &#8220;cartesian product&#8221;. Subtraits are defined for <code class="literal">Product</code>, called <code class="literal">ProductN</code>, for dimension <code class="literal">N</code> from 1 through 22.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">ScalaObject</code></p></td><td align="left" valign="top"><p><code class="literal">AnyRef</code></p></td><td align="left" valign="top"><p><span class="emphasis"><em>Mixin</em></span> trait added to all Scala reference type instances.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">Seq[+T]</code></p></td><td align="left" valign="top"><p><code class="literal">Collection[T]</code></p></td><td align="left" valign="top"><p>Trait for ordered collections.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">TupleN</code></p></td><td align="left" valign="top"><p><code class="literal">ProductN</code></p></td><td align="left" valign="top"><p>Separate case classes for arity <code class="literal">N</code> = 1 through 22. Tuples support the <span class="emphasis"><em>literal</em></span> syntax <code class="literal">(x1, x2, &#8230;, xN)</code>.</p></td></tr></tbody></table></div></div> <blockquote><p>The second line for the description for List[+T] has a typo. scala pacakge -&gt; scala package </p> &#8212; pgdad (2009-06-21 14:35:10) </blockquote> </div>urn:uuid:08b2052f-c612-3d82-b9f2-207a540dd0b7pgdad2009-06-21T14:35:10-07:00Comment on 'The Scala Type Hierarchy' <div>The Predef Object <p id="para_suppose_we_declare_a_var_of_">Briefly, a <code class="literal">Pair[A2,B2]</code>, for some <code class="literal">A2</code> and <code class="literal">B2</code>, is a <span class="emphasis"><em>subclass</em></span> of <code class="literal">Pair[A1,B1]</code>, for some <code class="literal">A1</code> and <code class="literal">B1</code>, if <code class="literal">A2</code> is a subtype of <code class="literal">A1</code> and <code class="literal">B2</code> is a subtype of <code class="literal">B1</code>. In <a class="xref" href="ch12.html#ParameterizedTypes" title="Understanding Parameterized Types">the section called &#8220;Understanding Parameterized Types&#8221;</a> in <a class="xref" href="ch12.html" title="Chapter&#160;12.&#160;The Scala Type System">Chapter&#160;12, <i>The Scala Type System</i></a>, we&#8217;ll discuss &#8216;+&#8217; and other type qualifiers in more detail.</p> <blockquote><p>'Typle[Ay, By] itself is a derived type of Typle[Ax, Bx]' should be 'Tuple2[Ay, By] ... Tuple2[Ax, Bx]' </p> &#8212; robcd (2009-06-21 12:32:31) </blockquote> </div>urn:uuid:2a2e87c0-ddf2-319d-a90f-9fee873c9e35robcd2009-06-21T12:32:31-07:00Comment on 'The Predef Object'