Comments on Programming Scalaurn:uuid:985ad4b9-eed8-3111-b211-c15a3f9ef0762009-08-11T20:58:40-07:00 <div>Overriding Members of Classes and Traits <p id="para_while_it_appears_that_we_are_c">While it appears that we are creating an instance of the trait with <code class="literal">new AbstractT2 &#8230;</code>, we are actually using an anonymous inner class that implicitly extends the trait. This script shows what happens when <code class="literal">inverse</code> is calculated</p> <blockquote><p>why is the new type definition of AbstractT2 an "anonymous inner class" and not just an "anonymous class"? </p> &#8212; maddalab (2009-08-02 16:42:08) </blockquote> </div>urn:uuid:894b9e7a-58ef-35b4-913a-62420382d63bmaddalab2009-08-02T16:42:08-07:00Comment on 'Overriding Members of Classes and Traits' <div>Case Classes <p id="para_the_second_circle_is_created_b">The second circle is created by copying the first and specifying a new radius. The <code class="literal">copy</code> method implementation that is generated by the compiler exploits the new named and default parameters in Scala version 2.8, which we discussed in <a class="xref" href="ch02.html#MethodDefaultAndNamedArguments" title="Method Default and Named Arguments (Scala Version 2.8)">the section called &#8220;Method Default and Named Arguments (Scala Version 2.8)&#8221;</a> in <a class="xref" href="ch02.html" title="Chapter&#160;2.&#160;Type Less, Do More">Chapter&#160;2, <i>Type Less, Do More</i></a>. The generated implementation of <code class="literal">Circle.copy</code> looks roughly like the following.</p> <blockquote><p>Doh! Thanks. </p> &#8212; deanwampler (2009-07-12 21:47:01) </blockquote> </div>urn:uuid:d19e2d96-8ea6-368d-b97b-e971be6e15bddeanwampler2009-07-12T21:47:01-07:00Comment on 'Case Classes' <div>Companion Objects <p id="para_this_code_compiles_fine_but_i">This code compiles fine, but if you attempt to invoke <code class="literal">Person.main</code>, using <strong class="userinput"><code>scala -cp ... objects.Person</code></strong>, you get the following error.</p> <blockquote><p>Yea, I think the typical "production" main would end up in its own object. The gotcha is most likely to happen when you put a "test" main in a class for convenience, but it doesn't work! </p> &#8212; deanwampler (2009-07-12 21:46:30) </blockquote> </div>urn:uuid:8cfb1ef6-052b-357e-8585-607f35ff24b5deanwampler2009-07-12T21:46:30-07:00Comment on 'Companion Objects' <div>Companion Objects <p id="para__button_unapply_takes_a_singl"><code class="literal">Button.unapply</code> takes a single <code class="literal">Button</code> argument and returns a <code class="literal">Some</code> wrapping the <code class="literal">label</code> value. This demonstrates the protocol for <code class="literal">unapply</code> methods. They return a <code class="literal">Some</code> wrapping the extracted fields. (We&#8217;ll see how to handle more than one field in a moment.)</p> <blockquote><p>Good point. Thanks. Need to decide on a good place for this. </p> &#8212; deanwampler (2009-07-12 21:45:08) </blockquote> </div>urn:uuid:3358870f-1f33-3b1e-b1a6-a03754bbeba2deanwampler2009-07-12T21:45:08-07:00Comment on 'Companion Objects' <div>Overriding Members of Classes and Traits <p id="para_fewer_object_oriented_language">Most OO languages allow you to override mutable fields (<code class="literal">var</code>). Fewer object-oriented languages allow you to define abstract fields or override concrete immutable fields (<code class="literal">val</code>). For example, it&#8217;s common for a base class constructor to initialize a mutable field and for a derived class constructor to change its value.</p> <blockquote><p>Thanks. will fix. </p> &#8212; deanwampler (2009-07-12 21:43:16) </blockquote> </div>urn:uuid:a99cba92-8d42-3250-ad0e-3eb5320c628edeanwampler2009-07-12T21:43:16-07:00Comment on 'Overriding Members of Classes and Traits' <div>Case Classes <p id="para_the_second_circle_is_created_b">The second circle is created by copying the first and specifying a new radius. The <code class="literal">copy</code> method implementation that is generated by the compiler exploits the new named and default parameters in Scala version 2.8, which we discussed in <a class="xref" href="ch02.html#MethodDefaultAndNamedArguments" title="Method Default and Named Arguments (Scala Version 2.8)">the section called &#8220;Method Default and Named Arguments (Scala Version 2.8)&#8221;</a> in <a class="xref" href="ch02.html" title="Chapter&#160;2.&#160;Type Less, Do More">Chapter&#160;2, <i>Type Less, Do More</i></a>. The generated implementation of <code class="literal">Circle.copy</code> looks roughly like the following.</p> <blockquote><p>The code should be "circle1 copy (radius = 4.0)". </p> &#8212; jesnor (2009-07-08 15:16:44) </blockquote> </div>urn:uuid:652d9d71-2e91-3c9b-b077-b144f775a2a0jesnor2009-07-08T15:16:44-07:00Comment on 'Case Classes' <div>Companion Objects <p id="para__button_unapply_takes_a_singl"><code class="literal">Button.unapply</code> takes a single <code class="literal">Button</code> argument and returns a <code class="literal">Some</code> wrapping the <code class="literal">label</code> value. This demonstrates the protocol for <code class="literal">unapply</code> methods. They return a <code class="literal">Some</code> wrapping the extracted fields. (We&#8217;ll see how to handle more than one field in a moment.)</p> <blockquote><p>Perhaps it would be interesting to point out that unapply can also return a Boolean, in which case it just matches, but does not extract anything? </p> <p>object isInt { def unapply(s: String): Boolean = s matches "\d+" } </p> <p>"54" match { case isInt() =&gt; println("Yep.") case _ =&gt; println("Oops!") } </p> &#8212; dcsobral (2009-07-03 22:56:04) </blockquote> </div>urn:uuid:d1d89235-87f7-3b1f-aa04-1e2ef1ce59e5dcsobral2009-07-03T22:56:04-07:00Comment on 'Companion Objects' <div>Companion Objects <p id="para_this_code_compiles_fine_but_i">This code compiles fine, but if you attempt to invoke <code class="literal">Person.main</code>, using <strong class="userinput"><code>scala -cp ... objects.Person</code></strong>, you get the following error.</p> <blockquote><p>We like putting our main methods in a Main object. Seems redundant but there's never confusion about which method to call. </p> &#8212; stevej (2009-06-24 22:11:17) </blockquote> </div>urn:uuid:19c9374f-094c-3ec7-a281-386029176facstevej2009-06-24T22:11:17-07:00Comment on 'Companion Objects' <div>Overriding Members of Classes and Traits <p id="para_fewer_object_oriented_language">Most OO languages allow you to override mutable fields (<code class="literal">var</code>). Fewer object-oriented languages allow you to define abstract fields or override concrete immutable fields (<code class="literal">val</code>). For example, it&#8217;s common for a base class constructor to initialize a mutable field and for a derived class constructor to change its value.</p> <blockquote><p>allow you <em>to</em> override </p> &#8212; robcd (2009-06-21 10:53:35) </blockquote> </div>urn:uuid:bf239d28-131d-33eb-89e8-32ef922e7f2brobcd2009-06-21T10:53:35-07:00Comment on 'Overriding Members of Classes and Traits'