Comments on Programming Scalaurn:uuid:1dfcecbc-384a-3cf2-b692-24ec633577c02009-08-11T20:59:03-07:00
<div>Effective Design of Traits <p id="para_however_since_a_trait_can_con">So, Scala handles many potential issues that arise when mixing the contributions of different traits into the set of possible states of an instance. Still, it’s important to consider how the contributions of different traits interact with each other.</p>
<blockquote><p>How are naming conflicts with mixin composition resolved? For example, if both B1 and B2 define (and implement) a method b3? If invoked on C, which would be called? One might expect the first one encountered in the linearized hierarchy. But can C's client also choose a different one? Or will the compiler complain?
</p>
— henrigerrits (2009-08-04 10:51:01)
</blockquote>
</div>urn:uuid:3a8c027c-f14c-36d6-8929-c3d4a2abf0dehenrigerrits2009-08-04T10:51:01-07:00Comment on 'Effective Design of Traits'
<div>Design Patterns <p id="para_wont_discuss_all_patterns">We won’t discuss all the well known patterns, such as those in <a class="xref" href="apa.html#GOF1995">[GOF1995]</a>.
A number of the GOF patterns are discussed at <a class="xref" href="apa.html#ScalaWikiPatterns">[ScalaWiki:Patterns]</a>, along with other
patterns that are somewhat specific to Scala.
Instead, we’ll discuss a few illustrative examples.
We’ll start by discussing a replacement for the <span class="emphasis"><em>Visitor Pattern</em></span> that
uses functional idioms and implicit conversations.
Then we’ll discuss a powerful way of implementing <span class="emphasis"><em>Dependency Injection</em></span> in
Scala using the <span class="emphasis"><em>Cake Pattern</em></span>.</p>
<blockquote><p>"implicit conversations" should be "implicit conversions"
</p>
— henrigerrits (2009-08-04 10:42:59)
</blockquote>
</div>urn:uuid:5d91ae2d-b1c8-3e34-9d1b-8f0b745d3e71henrigerrits2009-08-04T10:42:59-07:00Comment on 'Design Patterns'
<div>Design Patterns <p id="para_pattern_ideas_language_features_string_foreach">Actually, in this case, an implicit conversation is invoked to convert the <code class="literal">java.lang.String</code> to
a <code class="literal">RichString</code>, which has the <code class="literal">foreach</code> method. That’s an example of the pattern called <span class="emphasis"><em>pimp my library</em></span>, which we saw in <a class="xref" href="ch08.html#ImplicitConversions" title="Implicit Conversions">the section called “Implicit Conversions”</a>.</p>
<blockquote><p>"implicit conversation" should be "implicit conversion"
</p>
— henrigerrits (2009-08-04 10:42:17)
</blockquote>
</div>urn:uuid:0831565e-0b99-3b94-a507-701b02fb428fhenrigerrits2009-08-04T10:42:17-07:00Comment on 'Design Patterns'
<div>Scalable Abstractions <p id="para_encapsulates_encoded_strings">This example encapsulates handling of strings encoding comma-separated values (CSVs) or tab-separated values (TSVs). The <code class="literal">encodedstring</code> package exposes a trait <code class="literal">EncodedString</code> that is visible to clients. The concrete classes implementing CSVs and TSVs are declared <code class="literal">protected[encodedstring]</code> in the <code class="literal">encodedstring.impl</code> package. The trait defines two abstract <code class="literal">val</code> fields, one to hold the encoded <code class="literal">string</code>, which is protected from client access, and the other to hold the <code class="literal">separator</code> (<span class="emphasis"><em>e.g.</em></span>, a comma). Recall from <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> that abstract fields, like abstract types and methods, must be initialized in concrete instances. In this case, <code class="literal">string</code> will be defined through a concrete constructor and the <code class="literal">separator</code> is defined explicitly in the concrete classes, <code class="literal">CSV</code> and <code class="literal">TSV</code>.</p>
<blockquote><p>In the code, the concrete classes implementing CSVs and TSVs are declared private[encodedstring], not protected[encodedstring] as the paragraph states.
</p>
— jrduncans (2009-07-26 19:25:28)
</blockquote>
</div>urn:uuid:b8d3fd7d-bad1-3555-9822-052a4b257aa4jrduncans2009-07-26T19:25:28-07:00Comment on 'Scalable Abstractions'
<div>Enumerations vs. Pattern Matching <p id="para_redundant_connect">It is a bit redundant to have to use the same word twice in declarations like <code class="literal">val Connect = Value("Connect")</code>.</p>
<blockquote><p>Thanks.
</p>
— deanwampler (2009-07-12 22:13:32)
</blockquote>
</div>urn:uuid:3f25179f-2875-344a-aa69-12001c335070deanwampler2009-07-12T22:13:32-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Enumerations vs. Pattern Matching <p id="para_you_are_probably_accustomed_to">You are probably accustomed to using such names with Java or .NET enumerations.
Unfortunately, we can’t get those names from the
values in the Scala enumeration, at least given the way that we
declared <code class="literal">HttpMethod</code>. However, there are two ways we can change the implementation to
get name strings. In the first approach, we pass the name to <code class="literal">Value</code> when creating the
fields.</p>
<blockquote><p>thanks.
</p>
— deanwampler (2009-07-12 22:12:38)
</blockquote>
</div>urn:uuid:ba9a57b0-aaa6-3738-a908-37b323a61bf1deanwampler2009-07-12T22:12:38-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Enumerations vs. Pattern Matching <p id="para_the_second_use_of_value_is_a">The second use of <code class="literal">Value</code> is actually a call to a method. There is no
namespace collision between these two names. The line <code class="literal">val Connect,
Delete, Get, Head, Options, Post, Put, Trace = Value</code> defines the set
of values for the enumeration. The <code class="literal">Value</code> method is called for each
one. It creates a new <code class="literal">Enumeration.Value</code> for each one and adds it to
the managed set of values.</p>
<blockquote><p>Will fix
</p>
— deanwampler (2009-07-12 22:11:34)
</blockquote>
</div>urn:uuid:6b15dc94-1dda-3c88-a32c-90f61b93bb07deanwampler2009-07-12T22:11:34-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Annotations <p id="para_all_the_constructor_parameters">All the constructor parameters must be constant expressions, including
strings, class literals, Java enumerations, numerical expressions and
one-dimensional arrays of the same. However, the compiler also allows
annotation clauses with other arguments, such as boolean values and
maps, as shown in this example.</p>
<blockquote><p>I should add that the example compiles fine, but yes, having the keywords is not useful for the reasons discussed previously.
</p>
— deanwampler (2009-07-12 22:10:46)
</blockquote>
</div>urn:uuid:64cdaba1-d7d8-3d9e-81bc-859bc9cd0cd8deanwampler2009-07-12T22:10:46-07:00Comment on 'Annotations'
<div>Annotations <p id="para_all_the_constructor_parameters">All the constructor parameters must be constant expressions, including
strings, class literals, Java enumerations, numerical expressions and
one-dimensional arrays of the same. However, the compiler also allows
annotation clauses with other arguments, such as boolean values and
maps, as shown in this example.</p>
<blockquote><p>Bug in the following example. Thanks.
</p>
— deanwampler (2009-07-12 22:10:11)
</blockquote>
</div>urn:uuid:5a75e060-566c-3610-9ecf-cb788d13feaadeanwampler2009-07-12T22:10:11-07:00Comment on 'Annotations'
<div>Annotations <p id="para_all_the_constructor_parameters">All the constructor parameters must be constant expressions, including
strings, class literals, Java enumerations, numerical expressions and
one-dimensional arrays of the same. However, the compiler also allows
annotation clauses with other arguments, such as boolean values and
maps, as shown in this example.</p>
<blockquote><p>There is some description on "Did you notice that there is no val on uid?" above while the example below uses vals for annotation. This is a little confusing.
</p>
— parthm (2009-07-09 07:07:20)
</blockquote>
</div>urn:uuid:2e48190f-acec-3db5-b95e-f067698dd3e4parthm2009-07-09T07:07:20-07:00Comment on 'Annotations'
<div>Annotations <p id="para_did_you_notice_that_there_is_n">Did you notice that there is no <code class="literal">val</code> on <code class="literal">uid</code>? Why isn’t <code class="literal">uid</code> a
field? The reason is that the annotation’s data is not intended for
use by the program. Recall that it is metadata designed for external
tools to use, such as <strong class="userinput"><code>scalac</code></strong>. This also means that Scala annotations
have no way to define default values in version 2.7.X, as implicit
arguments don’t work. However, the new default arguments feature
in version 2.8.0 may work. (It is not yet implemented at the time of this writing).</p>
<blockquote><p>I don't know much about annotations but the scala compiler doesn't seem to product and error if val is used with annotation:
</p>
<pre><code>scala> class Bar(val x: Long) extends StaticAnnotation
defined class Bar
</code></pre><p>So is "val" silently ignored? Maybe it can be clarified.
</p>
— parthm (2009-07-09 07:01:04)
</blockquote>
</div>urn:uuid:1f0f74d0-a6cf-31aa-b755-4d41e30c623cparthm2009-07-09T07:01:04-07:00Comment on 'Annotations'
<div>Design Patterns <p id="para_let_s_build_a_simple_component">Let’s build a simple component model for an overly-simplified Twitter client. We want a configurable UI, a configurable local cache of past tweets, and a configurable connection to the Twitter service itself. Each of these “components” will be specified separately, along with a client component that will function as the “middleware” that ties the application together. The client component will depend on the other components. When we create a concrete client, we’ll configure in the concrete pieces of the other components that we need.</p>
<blockquote><p>I've got to reread this, but I wanted to see an example that showed unit testing and "production" configuration of a single trait/component.
</p>
<p>This pattern has always seemed a little abstract to me, and I hope this kind of example would ground it.
</p>
— jheintz (2009-07-01 11:44:15)
</blockquote>
</div>urn:uuid:d46c889f-6187-3fbc-858c-3956bf0fd5efjheintz2009-07-01T11:44:15-07:00Comment on 'Design Patterns'
<div>Enumerations vs. Pattern Matching <p id="para_redundant_connect">It is a bit redundant to have to use the same word twice in declarations like <code class="literal">val Connect = Value("Connect")</code>.</p>
<blockquote><p><em>to have to use</em> the same word twice
</p>
— robcd (2009-06-22 03:07:18)
</blockquote>
</div>urn:uuid:cd7b2c2f-dd89-3ed3-9080-604e87ba8d93robcd2009-06-22T03:07:18-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Enumerations vs. Pattern Matching <p id="para_you_are_probably_accustomed_to">You are probably accustomed to using such names with Java or .NET enumerations.
Unfortunately, we can’t get those names from the
values in the Scala enumeration, at least given the way that we
declared <code class="literal">HttpMethod</code>. However, there are two ways we can change the implementation to
get name strings. In the first approach, we pass the name to <code class="literal">Value</code> when creating the
fields.</p>
<blockquote><p>to doing <- using such names
</p>
— robcd (2009-06-22 03:03:11)
</blockquote>
</div>urn:uuid:24780786-ecea-31bc-bd43-9faa3ef152d7robcd2009-06-22T03:03:11-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Enumerations vs. Pattern Matching <p id="para_the_second_use_of_value_is_a">The second use of <code class="literal">Value</code> is actually a call to a method. There is no
namespace collision between these two names. The line <code class="literal">val Connect,
Delete, Get, Head, Options, Post, Put, Trace = Value</code> defines the set
of values for the enumeration. The <code class="literal">Value</code> method is called for each
one. It creates a new <code class="literal">Enumeration.Value</code> for each one and adds it to
the managed set of values.</p>
<blockquote><p>The first usage is actual<em>ly</em>
</p>
— robcd (2009-06-22 02:59:21)
</blockquote>
</div>urn:uuid:e1cefa48-a33b-36f4-a752-53aa613b71d0robcd2009-06-22T02:59:21-07:00Comment on 'Enumerations vs. Pattern Matching'
<div>Annotations <p id="para_note_the_throws_annotation_">Note the <code class="literal">@throws</code> annotation applied to the <code class="literal">print</code> method. The
argument to the annotation constructor is a single
<code class="literal">java.lang.Class[Any]</code> object, in this case, <code class="literal">classOf[IOException]</code>.
The Java IO API methods used by <code class="literal">print</code> and the private method <code class="literal">loop</code>
might throw this exception.</p>
<blockquote><p>Good point. We want the code to be "good". I'll add the try/finally clause.
</p>
— deanwampler (2009-06-08 19:33:22)
</blockquote>
</div>urn:uuid:2d37f932-3fef-3b38-a10f-0186d0bbe794deanwampler2009-06-08T19:33:22-07:00Comment on 'Annotations'