Comments on Programming Scalaurn:uuid:926b6d3c-b92c-32d2-b45c-d778313cfc4a2009-08-11T20:58:08-07:00 <div>Inferring Type Information <p id="para_script_return_error">Running this script returns the following error.</p> <blockquote><p>You also forgot to change the comment text. It still refers to the "joiner" example above. </p> &#8212; derekmahar (2009-08-05 16:00:15) </blockquote> </div>urn:uuid:7a4c393e-e277-3c7e-9657-8240304ed8eaderekmahar2009-08-05T16:00:15-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_by_the_way_the_that_was_">By the way, where did that <code class="literal">()</code> come from that was printed before we fixed the bug? It is actually the real name of the <span class="emphasis"><em>singleton</em></span> instance of the <code class="literal">Unit</code> type! (This name is a functional programming convention.)</p> <blockquote><p><i>When the return type of a method is inferred and you don’t use an equals sign</i> </p> <p>Implies that the return type can be specified without an equals sign as </p> <p>def two():Int { 2 } </p> <p>which is incorrect, results in a compile error </p> &#8212; maddalab (2009-07-26 16:25:05) </blockquote> </div>urn:uuid:64782282-93b5-3df6-8b21-d0e043933c80maddalab2009-07-26T16:25:05-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_second_joiner_method_calls_the_first">Since the <span class="emphasis"><em>second</em></span> <code class="literal">joiner</code> method calls the first, it requires an explicit <code class="literal">String</code> return type. It should look like this.</p> <blockquote><p>Some where along the way you went from a method that has an argument of List[String] to a method that has an var age String* </p> <p>Code def joiner(strings: List[String]) = joiner(strings, " ") </p> <p>Compiler Errors def joiner(strings: String*) = joiner(strings.toList) </p> &#8212; maddalab (2009-07-26 14:27:36) </blockquote> </div>urn:uuid:bead4971-1ba2-3929-bd4b-d5b2d868d302maddalab2009-07-26T14:27:36-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para_why_is_this_useful_first_if_">Why is this useful? First, if you choose good names for the method arguments, then your calls to those methods document each argument with a name. For example, compare the two lines with comments <code class="literal">#1</code> and <code class="literal">#2</code>. In the first line, it may not be obvious what the second, <code class="literal">" "</code> argument is for. In the second case, we supply the name <code class="literal">separator</code>, which suggests the purpose of the argument.</p> <blockquote><p>Dope, Spoke to soon, Sorry about that </p> &#8212; maddalab (2009-07-26 13:39:28) </blockquote> </div>urn:uuid:06228dcb-c8ce-3773-bd95-2f53bdcb98b5maddalab2009-07-26T13:39:28-07:00Comment on 'Method Declarations' <div>Method Declarations <p id="para_why_is_this_useful_first_if_">Why is this useful? First, if you choose good names for the method arguments, then your calls to those methods document each argument with a name. For example, compare the two lines with comments <code class="literal">#1</code> and <code class="literal">#2</code>. In the first line, it may not be obvious what the second, <code class="literal">" "</code> argument is for. In the second case, we supply the name <code class="literal">separator</code>, which suggests the purpose of the argument.</p> <blockquote><p>Example should include </p> <p>StringUtils.joiner(separator="|", strings=List("Programming", "Scala")) </p> <p>with named argument one could avoid positional arguments entirely </p> &#8212; maddalab (2009-07-26 13:38:17) </blockquote> </div>urn:uuid:c6d9d674-064d-399c-bab1-70cad7322379maddalab2009-07-26T13:38:17-07:00Comment on 'Method Declarations' <div>Organizing Code in Files and Namespaces <p id="para_the_following_example_defines_">The following example defines a class <code class="literal">MyClass</code> in a package <code class="literal">com.example.mypkg</code> using the conventional Java syntax.</p> <blockquote><p>Might be nice to indicate that Java syntax also works the Java way: it applies to whole file, it has to come first, etc. At first I wasn't certain if it worked more like the Ruby private keyword, where the package would apply until you declared another. </p> &#8212; jrduncans (2009-07-14 19:51:51) </blockquote> </div>urn:uuid:c004db72-7576-38ea-b887-3d4b4e7c369djrduncans2009-07-14T19:51:51-07:00Comment on 'Organizing Code in Files and Namespaces' <div>Importing Types and Their Members <p id="para_there_s_one_other_important_th">There&#8217;s one other important thing to know about imports; they are <span class="emphasis"><em>relative</em></span>. The following imports are all the same.</p> <blockquote><p>This paragraph is confusing. It would be fine to note, that it's not relative in a way e. g. hrefs are relative, but instead "a package reference starting in a name p will be looked up in the closest enclosing scope that defines a member named p." (Scala Language Reference p. 118) </p> &#8212; hotdog (2009-07-14 09:24:43) </blockquote> </div>urn:uuid:8a51291e-003e-33fa-a1ce-d909b2e311fahotdog2009-07-14T09:24:43-07:00Comment on 'Importing Types and Their Members' <div>Option, Some, and None: Avoiding nulls <p id="para_the_contains_method_is_also_">The <code class="literal">contains</code> method is also defined for <code class="literal">Map</code>. It returns <code class="literal">true</code> if the map contains a value for the specified key. The <code class="literal">getValue</code> method is intended to be an internal method that retrieves the value from the underlying storage, whatever it is.</p> <blockquote><p>Some is a case class, so the new keyword is unnecessary. If you just don't want to get into that yet, it does make sense to leave it in. </p> &#8212; GatesDA (2009-07-14 05:08:55) </blockquote> </div>urn:uuid:1880b6e5-80ef-35bd-af2f-0e181eb660a7GatesDA2009-07-14T05:08:55-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Organizing Code in Files and Namespaces <p id="para_scala_adopts_the_package_conce">Scala adopts the package concept that Java uses for namespaces, but Scala offers a more flexible syntax. Just as file names don&#8217;t have to match the type names, the package structure does not have to match the directory structure. So, you can define packages in files independent of their &#8220;physical&#8221; location.</p> <blockquote><p>We get into this later on. </p> &#8212; deanwampler (2009-07-12 19:00:06) </blockquote> </div>urn:uuid:dfb70972-ae8c-3bc7-a5e8-e1d459b65f1adeanwampler2009-07-12T19:00:06-07:00Comment on 'Organizing Code in Files and Namespaces' <div>Option, Some, and None: Avoiding nulls <p id="para_we_also_show_the_alternative_m">We also show the alternative method, <code class="literal">getOrElse</code>, in the last two <code class="literal">println</code> statements. This method returns either the value in the <code class="literal">Option</code>, if it is a <code class="literal">Some</code> instance, or it returns the second argument we passed to <code class="literal">getOrElse</code>, if it is a <code class="literal">None</code> instance. In other words, the second argument to <code class="literal">getOrElse</code> functions as the default return value.</p> <blockquote><p>Will improve the wording. thanks. </p> &#8212; deanwampler (2009-07-12 18:59:46) </blockquote> </div>urn:uuid:57928565-dd07-3cf9-8fea-80d366290af9deanwampler2009-07-12T18:59:46-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Option, Some, and None: Avoiding nulls <p id="para_most_languages_have_a_special_">Most languages have a special keyword or object that&#8217;s assigned to reference variables when there&#8217;s nothing else for them to refer to. In Java, this is <code class="literal">null</code>; in Ruby, it&#8217;s <code class="literal">nil</code>. In Java, <code class="literal">null</code> is a keyword, not an object, and thus it&#8217;s illegal to call any methods on it. But this is a confusing choice on the language designer&#8217;s part. Why return a keyword when the programmer expects an object?</p> <blockquote><p>Yes. I didn't mean to imply it wasn't ;) </p> &#8212; deanwampler (2009-07-12 18:57:35) </blockquote> </div>urn:uuid:97dcc8f0-7b41-3fc4-8cb4-0f9b056d515cdeanwampler2009-07-12T18:57:35-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Tuples <p id="para_running2">Running this script with <strong class="userinput"><code>scala</code></strong> produces the following output.</p> <blockquote><p>True. I went for the "dumber", but slightly clearer approach here. </p> &#8212; deanwampler (2009-07-12 18:56:51) </blockquote> </div>urn:uuid:40410792-65ac-3924-994b-75feee7a7012deanwampler2009-07-12T18:56:51-07:00Comment on 'Tuples' <div>Literals <p id="para_copy_and_past_these_strings_in">Copy and paste these strings into the <strong class="userinput"><code>scala</code></strong> interpreter. Do the same for the previous string examples. How are they interpreted differently?</p> <blockquote><p>Thanks. Will fix. </p> &#8212; deanwampler (2009-07-12 18:55:29) </blockquote> </div>urn:uuid:54118ea8-dabb-346e-ad34-e51a1355d8a2deanwampler2009-07-12T18:55:29-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_why_did_the_second_command_pri">Why did the second command print <code class="literal">()</code> instead of <code class="literal">4</code>? Look carefully at what the <strong class="userinput"><code>scala</code></strong> interpreter said the first command returned, <code class="literal">double (Int)Unit</code>. We defined a method named <code class="literal">double</code> that takes an <code class="literal">Int</code> argument and returns <code class="literal">Unit</code>. The method doesn&#8217;t return an <code class="literal">Int</code> as we would expect.</p> <blockquote><p>Right, absent minded typing... changing to void. </p> &#8212; deanwampler (2009-07-12 18:53:41) </blockquote> </div>urn:uuid:6d5d50a6-213d-3789-bb7a-18484291c35fdeanwampler2009-07-12T18:53:41-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_declare_method_returns">Another way to prevent these problems is to always declare return types for methods, especially when defining methods for a public API. Let&#8217;s revisit our <code class="literal">StringUtil</code> example and see why explicit declarations are a good idea (adapted from <a class="xref" href="apa.html#Smith2009a">[Smith2009a]</a>).</p> <blockquote><p>Fixed. </p> &#8212; deanwampler (2009-07-12 18:47:52) </blockquote> </div>urn:uuid:d0c2a268-8010-32c4-9813-8b140778abf6deanwampler2009-07-12T18:47:52-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_script_return_error">Running this script returns the following error.</p> <blockquote><p>Thanks. changed the example, but not the text. Doh! </p> &#8212; deanwampler (2009-07-12 18:41:05) </blockquote> </div>urn:uuid:dfd2948b-650a-3d79-bdd7-3bb50d8045eddeanwampler2009-07-12T18:41:05-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_we_intended_for_makelist_to_">We intended for <code class="literal">makeList</code> to return a <code class="literal">List[String]</code>, but when <code class="literal">strings.length</code> equals zero, we returned <code class="literal">List(0)</code>, incorrectly &#8220;assuming&#8221; that this expression is the correct way to create an empty list. In fact, we returned a <code class="literal">List[Int]</code> with one element, <code class="literal">0</code>. We should have returned <code class="literal">List()</code>. Since the <code class="literal">else</code> expression returns a <code class="literal">List[String]</code>, the result of <code class="literal">strings.toList</code>, the inferred return type for the method is the closest common super type of <code class="literal">List[Int]</code> and <code class="literal">List[String]</code>, which is <code class="literal">List[Any]</code>. Note that the compilation error doesn&#8217;t occur in the function definition. We only see it when we attempt to assign the value returned from <code class="literal">makeList</code> to a <code class="literal">List[String]</code> variable.</p> <blockquote><p>Thanks. See my previous comment reply... </p> &#8212; deanwampler (2009-07-12 18:40:47) </blockquote> </div>urn:uuid:801522ed-5d5b-3a49-aded-dd0b63d7d29ddeanwampler2009-07-12T18:40:47-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_two_joiner_methods_conca">The two <code class="literal">joiner</code> methods concatenate a <code class="literal">List</code> of strings together. The first method also takes an argument for the separator string. The second method calls the first with a &#8220;default&#8221; separator of a single space.</p> <blockquote><p>Doh. You're right. I changed the example and didn't update the text. Will fix. Thanks. </p> &#8212; deanwampler (2009-07-12 18:34:44) </blockquote> </div>urn:uuid:cf8b311e-e7bf-3d56-b071-46fde42a39e1deanwampler2009-07-12T18:34:44-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para_the_second_method_is_nested_in">The second method calls itself recursively, passing an <code class="literal">accumulator</code> parameter, where the result of the calculation is &#8220;accumulated&#8221;. Note that we return the accumulated value when the counter <code class="literal">i</code> reaches 1. (We&#8217;re ignoring invalid negative integers. The function actually returns 1 for <code class="literal">i &lt; 0</code>.) After the definition of the nested method, <code class="literal">factorial</code> calls it with the passed-in value <code class="literal">i</code> and the initial accumulator value of 1.</p> <blockquote><p>Will fix these. Thanks. </p> &#8212; deanwampler (2009-07-12 18:31:20) </blockquote> </div>urn:uuid:295301f6-add8-36fa-b625-e786f6a48434deanwampler2009-07-12T18:31:20-07:00Comment on 'Method Declarations' <div>Method Declarations <p id="para__optionaluserprofileinfo_repr"><code class="literal">OptionalUserProfileInfo</code> represents all the &#8220;optional&#8221; user profile data in your next Web 2.0, social networking site. It defines default values for all its fields. The script creates instances with zero or more named parameters. The order of those parameters is arbitrary.</p> <blockquote><p>Thanks. We caught and fixed that one. </p> &#8212; deanwampler (2009-07-12 18:29:20) </blockquote> </div>urn:uuid:50edcf96-1c84-3dd1-ac24-15c82bd3487fdeanwampler2009-07-12T18:29:20-07:00Comment on 'Method Declarations' <div>Method Declarations <p id="para_multiple_arg_lists">We said &#8220;optional argument lists&#8221;, meaning more than one. Scala lets you define more than one argument list for a method. This is required for <span class="emphasis"><em>currying</em></span> methods, which we&#8217;ll discuss in <a class="xref" href="ch08.html#Currying" title="Currying">the section called &#8220;Currying&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>. It is also very useful for defining your own domain-specific languages (DSLs), as we&#8217;ll see in <a class="xref" href="ch11.html" title="Chapter&#160;11.&#160;Domain-Specific Languages in Scala">Chapter&#160;11, <i>Domain-Specific Languages in Scala</i></a>. Note that each argument list is surrounded by parentheses and the arguments are separated by commas.</p> <blockquote><p>Will fix. </p> &#8212; deanwampler (2009-07-12 18:27:33) </blockquote> </div>urn:uuid:11558930-2294-3fc8-8fe8-98124c37b712deanwampler2009-07-12T18:27:33-07:00Comment on 'Method Declarations' <div>Variable Declarations <p id="para_scala_also_requires_you_to_ini">Scala also requires you to initialize a <code class="literal">var</code> when it is declared. You can assign a new value to a <code class="literal">var</code> as often as you want. Again, to be precise, the <code class="literal">stockPrice</code> reference can be changed to point to a different <code class="literal">Double</code> object (<span class="emphasis"><em>e.g.</em></span>, <code class="literal">10.</code>). In this case, the object that <code class="literal">stockPrice</code> refers to can&#8217;t be changed, because <code class="literal">Doubles</code> in Scala are immutable.</p> <blockquote><p>Adding "in Scala" -&gt; "... Doubles in Scala are immutable." Not a complete explanation, but a reasonable change at this late date and it implies that Doubles are immutable by design in Scala. </p> &#8212; deanwampler (2009-07-12 18:26:42) </blockquote> </div>urn:uuid:daf80634-5fa6-3f1b-954e-e1000db304c3deanwampler2009-07-12T18:26:42-07:00Comment on 'Variable Declarations' <div>Inferring Type Information <p id="para_why_did_the_second_command_pri">Why did the second command print <code class="literal">()</code> instead of <code class="literal">4</code>? Look carefully at what the <strong class="userinput"><code>scala</code></strong> interpreter said the first command returned, <code class="literal">double (Int)Unit</code>. We defined a method named <code class="literal">double</code> that takes an <code class="literal">Int</code> argument and returns <code class="literal">Unit</code>. The method doesn&#8217;t return an <code class="literal">Int</code> as we would expect.</p> <blockquote><p>Isn't it more common to make the comparison of Unit to Void? </p> &#8212; jwolski (2009-07-11 10:46:48) </blockquote> </div>urn:uuid:a94491ca-d5b3-3895-a76f-2b471916475ejwolski2009-07-11T10:46:48-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_two_joiner_methods_conca">The two <code class="literal">joiner</code> methods concatenate a <code class="literal">List</code> of strings together. The first method also takes an argument for the separator string. The second method calls the first with a &#8220;default&#8221; separator of a single space.</p> <blockquote><p>The code listing (method-overloaded-return-script.scala) seems out of sync with the text: (1) the second joiner is taking a list of strings (2) the first joiner doesn't have a default value for separator (which doesn't match the "def joiner(strings: String*) = joiner(strings.toList)" below </p> &#8212; myee (2009-07-11 10:37:48) </blockquote> </div>urn:uuid:61041f24-5558-31b7-8d73-7632b28dfc53myee2009-07-11T10:37:48-07:00Comment on 'Inferring Type Information' <div>Organizing Code in Files and Namespaces <p id="para_scala_adopts_the_package_conce">Scala adopts the package concept that Java uses for namespaces, but Scala offers a more flexible syntax. Just as file names don&#8217;t have to match the type names, the package structure does not have to match the directory structure. So, you can define packages in files independent of their &#8220;physical&#8221; location.</p> <blockquote><p>Maybe it should be mentioned here that when you compile a file with a package definition in it, the scalac compiler creates a sibdirectory and puts the .class files in it (I don't think that javac does the same ). </p> &#8212; bieffe (2009-07-10 03:24:08) </blockquote> </div>urn:uuid:63a41ae3-63ad-3573-99fc-3423f1c91021bieffe2009-07-10T03:24:08-07:00Comment on 'Organizing Code in Files and Namespaces' <div>Inferring Type Information <p id="para_recall_from_chapter_1_that_sca">Recall from Chapter 1 that Scala uses square brackets (<code class="literal">[&#8230;]</code>) for generic type parameters. We specify <code class="literal">Map[Integer, String]</code> on the left-hand side of the equals sign. (We are sticking with Java types for the example.) On the right-hand side, we instantiate the actual type we want, a <code class="literal">HashMap</code>, but we don&#8217;t have to repeat the type parameters.</p> <blockquote><p>@Tim, fair point. I was sticking with Java types, since that's how I started out in this bit. Should say as much </p> &#8212; deanwampler (2009-07-09 15:42:43) </blockquote> </div>urn:uuid:1cb13538-8e72-376d-ab01-5ba9a0d521b2deanwampler2009-07-09T15:42:43-07:00Comment on 'Inferring Type Information' <div>Semicolons <p id="para_why_no_curly_braces">You might wonder why you don&#8217;t need curly braces (<code class="literal">{&#8230;}</code>) around the two statements after the <code class="literal">case &#8230; =&gt;</code> line. You can put them in if you want, but the compiler knows when you&#8217;ve reached the end of the &#8220;block&#8221; when it finds the next <code class="literal">case</code> clause or the curly brace (<code class="literal">}</code>) that ends the enclosing block for all the <code class="literal">case</code> clauses.</p> <blockquote><p>Most people leave them out when they can. Will consider adding more verbage about that. </p> &#8212; deanwampler (2009-07-09 15:39:46) </blockquote> </div>urn:uuid:74f666fa-fcd8-3c0b-9dfb-92922a67942adeanwampler2009-07-09T15:39:46-07:00Comment on 'Semicolons' <div>Variable Declarations <p id="para_scala_also_requires_you_to_ini">Scala also requires you to initialize a <code class="literal">var</code> when it is declared. You can assign a new value to a <code class="literal">var</code> as often as you want. Again, to be precise, the <code class="literal">stockPrice</code> reference can be changed to point to a different <code class="literal">Double</code> object (<span class="emphasis"><em>e.g.</em></span>, <code class="literal">10.</code>). In this case, the object that <code class="literal">stockPrice</code> refers to can&#8217;t be changed, because <code class="literal">Doubles</code> in Scala are immutable.</p> <blockquote><p>...or explain briefly why Doubles are immutable. </p> &#8212; derekmahar (2009-07-09 15:16:33) </blockquote> </div>urn:uuid:88573ec1-3b2a-3842-8c7d-72029754bf50derekmahar2009-07-09T15:16:33-07:00Comment on 'Variable Declarations' <div>Variable Declarations <p id="para_scala_allows_you_to_decide_whe">Scala allows you to decide whether a variable is immutable (read-only) or not (read-write) when you declare it. An immutable &#8220;variable&#8221; is declared with the keyword <code class="literal">val</code> (think <span class="emphasis"><em>value object</em></span>).</p> <blockquote><p>@Marek: I think the variable is actually immutable. The variable is the reference cell which refers to the object and the name is the identifier assigned to this cell. I agree with you that the name associated with the variable never changes (though you can create aliases using other references), so the name is immutable. I also agree with you that the object to which the variable refers is mutable. However, I disagree that the variable contents, which is a reference to the object and not the object itself is mutable. The "val" keyword applies to the variable, not to the name or the object. </p> &#8212; derekmahar (2009-07-09 14:40:55) </blockquote> </div>urn:uuid:0a08a577-60d3-32cf-8346-f95652f94c78derekmahar2009-07-09T14:40:55-07:00Comment on 'Variable Declarations' <div>Method Declarations <p id="para_the_second_method_is_nested_in">The second method calls itself recursively, passing an <code class="literal">accumulator</code> parameter, where the result of the calculation is &#8220;accumulated&#8221;. Note that we return the accumulated value when the counter <code class="literal">i</code> reaches 1. (We&#8217;re ignoring invalid negative integers. The function actually returns 1 for <code class="literal">i &lt; 0</code>.) After the definition of the nested method, <code class="literal">factorial</code> calls it with the passed-in value <code class="literal">i</code> and the initial accumulator value of 1.</p> <blockquote><p>"The function actually return 1" =&gt; "The function actually returns 1" "the accumulator value of 1" =&gt; "an initial accumulator value of 1" </p> &#8212; GatesDA (2009-07-09 10:08:38) </blockquote> </div>urn:uuid:76129409-ca39-334f-bf91-feecc17856a2GatesDA2009-07-09T10:08:38-07:00Comment on 'Method Declarations' <div>Inferring Type Information <p id="para_we_intended_for_makelist_to_">We intended for <code class="literal">makeList</code> to return a <code class="literal">List[String]</code>, but when <code class="literal">strings.length</code> equals zero, we returned <code class="literal">List(0)</code>, incorrectly &#8220;assuming&#8221; that this expression is the correct way to create an empty list. In fact, we returned a <code class="literal">List[Int]</code> with one element, <code class="literal">0</code>. We should have returned <code class="literal">List()</code>. Since the <code class="literal">else</code> expression returns a <code class="literal">List[String]</code>, the result of <code class="literal">strings.toList</code>, the inferred return type for the method is the closest common super type of <code class="literal">List[Int]</code> and <code class="literal">List[String]</code>, which is <code class="literal">List[Any]</code>. Note that the compilation error doesn&#8217;t occur in the function definition. We only see it when we attempt to assign the value returned from <code class="literal">makeList</code> to a <code class="literal">List[String]</code> variable.</p> <blockquote><p>The sentence "We intended for makeList to return a List[String], but the case 0 expression actually returns a List[Int]" mentions a "case 0" which does not exist, the example above contains an if-clause. Seems to me as if the example has changed. I would propose to use the comment "#1" in the example (which isn't referred to for any reason) and write instead "We intended for makeList to return a List[String], but the if-clause marked with "#1" actually returns a List[Int]". </p> &#8212; Marius (2009-07-09 07:52:45) </blockquote> </div>urn:uuid:e6f1b0ef-972e-3bb8-8f75-8bc4cf72cb7bMarius2009-07-09T07:52:45-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para__optionaluserprofileinfo_repr"><code class="literal">OptionalUserProfileInfo</code> represents all the &#8220;optional&#8221; user profile data in your next Web 2.0, social networking site. It defines default values for all its fields. The script creates instances with zero or more named parameters. The order of those parameters is arbitrary.</p> <blockquote><pre><code>personalWebSite: String = OptionalUserProfileInfo.UnknownWebSite) </code></pre><p>instead of? personalWebSite = OptionalUserProfileInfo.UnknownWebSite) </p> <p>Haven't tried this on the scala 2.8 shell. </p> &#8212; parthm (2009-07-06 05:56:04) </blockquote> </div>urn:uuid:8b9b5ff1-b8be-3616-b5aa-44f3ff5b1fb7parthm2009-07-06T05:56:04-07:00Comment on 'Method Declarations' <div>Semicolons <p id="para_why_no_curly_braces">You might wonder why you don&#8217;t need curly braces (<code class="literal">{&#8230;}</code>) around the two statements after the <code class="literal">case &#8230; =&gt;</code> line. You can put them in if you want, but the compiler knows when you&#8217;ve reached the end of the &#8220;block&#8221; when it finds the next <code class="literal">case</code> clause or the curly brace (<code class="literal">}</code>) that ends the enclosing block for all the <code class="literal">case</code> clauses.</p> <blockquote><p>Is there a style recommendation on using or not using braces with case that can be put in here? The preferred approach by the scala community. </p> &#8212; parthm (2009-07-06 05:37:59) </blockquote> </div>urn:uuid:2be873d9-f7e8-38a6-a9af-a95febf5259dparthm2009-07-06T05:37:59-07:00Comment on 'Semicolons' <div>Tuples <p id="para_scala_supports__tuples__a_gr">Scala, supports <span class="emphasis"><em>tuples</em></span>, a grouping of two or more items, usually created with the literal syntax of a comma-separated list of the items inside parentheses, <span class="emphasis"><em>e.g.,</em></span> <code class="literal">(x1, x2, &#8230;)</code>. The types of the <code class="literal">x</code><sub>i</sub> elements are unrelated to each other, you can mix and match types. These literal &#8220;groupings&#8221; are instantiated as <code class="literal">scala.TupleN</code> instances, where the <code class="literal">N</code> is the number of items in the tuple. The Scala API defines separate <code class="literal">TupleN</code> classes for N between 1 and 22, inclusive. Tuple instances are immutable, <span class="emphasis"><em>first-class</em></span> values, so you can assign them to variables, pass them as values, and return them from methods.</p> <blockquote><p>Apparently, this: <console>:5: error: value Tuple23 is not a member of package scala </p> &#8212; diathesis (2009-06-23 10:17:42) </blockquote> </div>urn:uuid:eeb10216-9157-317a-aea0-334dbaf14289diathesis2009-06-23T10:17:42-07:00Comment on 'Tuples' <div>Tuples <p id="para_scala_supports__tuples__a_gr">Scala, supports <span class="emphasis"><em>tuples</em></span>, a grouping of two or more items, usually created with the literal syntax of a comma-separated list of the items inside parentheses, <span class="emphasis"><em>e.g.,</em></span> <code class="literal">(x1, x2, &#8230;)</code>. The types of the <code class="literal">x</code><sub>i</sub> elements are unrelated to each other, you can mix and match types. These literal &#8220;groupings&#8221; are instantiated as <code class="literal">scala.TupleN</code> instances, where the <code class="literal">N</code> is the number of items in the tuple. The Scala API defines separate <code class="literal">TupleN</code> classes for N between 1 and 22, inclusive. Tuple instances are immutable, <span class="emphasis"><em>first-class</em></span> values, so you can assign them to variables, pass them as values, and return them from methods.</p> <blockquote><p>What happens if you declare a tuple of size &gt; 22? </p> &#8212; diathesis (2009-06-23 10:15:50) </blockquote> </div>urn:uuid:88379706-68b7-3c86-93c0-639ddc0487b9diathesis2009-06-23T10:15:50-07:00Comment on 'Tuples' <div>Tuples <p id="para_scala_supports__tuples__a_gr">Scala, supports <span class="emphasis"><em>tuples</em></span>, a grouping of two or more items, usually created with the literal syntax of a comma-separated list of the items inside parentheses, <span class="emphasis"><em>e.g.,</em></span> <code class="literal">(x1, x2, &#8230;)</code>. The types of the <code class="literal">x</code><sub>i</sub> elements are unrelated to each other, you can mix and match types. These literal &#8220;groupings&#8221; are instantiated as <code class="literal">scala.TupleN</code> instances, where the <code class="literal">N</code> is the number of items in the tuple. The Scala API defines separate <code class="literal">TupleN</code> classes for N between 1 and 22, inclusive. Tuple instances are immutable, <span class="emphasis"><em>first-class</em></span> values, so you can assign them to variables, pass them as values, and return them from methods.</p> <blockquote><p>You say "two or more items" at first, then "N between 1 and 22, inclusive". It would be nice to reconcile these statements. Does Scala not have the (1,) syntax for a single-element tuple? </p> <p>Also, you say "first-class" as if it's of extreme importance, but we newbie readers have no idea what you're talking about. Either leave it out to be dealt with later, or give us a hint as to what the alternative would be. </p> &#8212; TimMacEachern (2009-06-23 10:14:49) </blockquote> </div>urn:uuid:d5e204bc-2810-3d38-a80c-3fc399ff09a9TimMacEachern2009-06-23T10:14:49-07:00Comment on 'Tuples' <div>Literals <p id="para_floating_point_literals_are_ex">Floating point literals are expressions with zero or more digits, followed by a period <code class="literal">.</code>, followed by zero or more digits. If there are no digits before the period, <span class="emphasis"><em>i.e.,</em></span> the number is less than 1.0, then there must be one or more digits after the period. For <code class="literal">Float</code> literals, append the <code class="literal">F</code> or <code class="literal">f</code> character at the end of the literal. Otherwise, a <code class="literal">Double</code> is assumed. You can optionally append a <code class="literal">D</code> or <code class="literal">d</code> for a <code class="literal">Double</code>.</p> <blockquote><p>"are expresses" should presumably be "are expressed" </p> &#8212; diathesis (2009-06-23 10:09:26) </blockquote> </div>urn:uuid:6e2623cf-6132-3521-a461-78c61c6519b6diathesis2009-06-23T10:09:26-07:00Comment on 'Literals' <div>Literals <p id="para_floating_point_literals_are_ex">Floating point literals are expressions with zero or more digits, followed by a period <code class="literal">.</code>, followed by zero or more digits. If there are no digits before the period, <span class="emphasis"><em>i.e.,</em></span> the number is less than 1.0, then there must be one or more digits after the period. For <code class="literal">Float</code> literals, append the <code class="literal">F</code> or <code class="literal">f</code> character at the end of the literal. Otherwise, a <code class="literal">Double</code> is assumed. You can optionally append a <code class="literal">D</code> or <code class="literal">d</code> for a <code class="literal">Double</code>.</p> <blockquote><p>expresses =&gt; expressed </p> &#8212; TimMacEachern (2009-06-23 09:56:29) </blockquote> </div>urn:uuid:ab255d10-02fe-3aba-b911-dfdce969a526TimMacEachern2009-06-23T09:56:29-07:00Comment on 'Literals' <div>Literals <p id="para_integer_literals_can_be_expres">Integer literals can be expresses in decimal, hexadecimal, or octal. The details are summarized in <a class="xref" href="ch02.html#integer-literals-table" title="Table&#160;2.1.&#160;Integer literals.">Table&#160;2.1, &#8220;Integer literals.&#8221;</a>.</p> <blockquote><p>expresses =&gt; expressed </p> &#8212; TimMacEachern (2009-06-23 09:53:40) </blockquote> </div>urn:uuid:c2ffcebd-825f-3536-8792-f37fb04f3db8TimMacEachern2009-06-23T09:53:40-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_actually_for_this_particular_">Actually, for this particular script, an alternative fix is to remove the <code class="literal">return</code> keyword from the line. It is not needed for the code to work properly, but it illustrates our point.</p> <blockquote><p>Seems like a discussion as to why a return requires type annotation would be useful. </p> &#8212; diathesis (2009-06-23 09:40:05) </blockquote> </div>urn:uuid:461310c4-0ae5-3c2e-b9c1-0574acaf5071diathesis2009-06-23T09:40:05-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_declare_method_returns">Another way to prevent these problems is to always declare return types for methods, especially when defining methods for a public API. Let&#8217;s revisit our <code class="literal">StringUtil</code> example and see why explicit declarations are a good idea (adapted from <a class="xref" href="apa.html#Smith2009a">[Smith2009a]</a>).</p> <blockquote><p>You didn't have a StringUtil example, you had a NameUtil example. </p> &#8212; TimMacEachern (2009-06-23 08:53:46) </blockquote> </div>urn:uuid:042b682e-d76c-3c97-8edc-d8606378f84fTimMacEachern2009-06-23T08:53:46-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_recall_from_chapter_1_that_sca">Recall from Chapter 1 that Scala uses square brackets (<code class="literal">[&#8230;]</code>) for generic type parameters. We specify <code class="literal">Map[Integer, String]</code> on the left-hand side of the equals sign. (We are sticking with Java types for the example.) On the right-hand side, we instantiate the actual type we want, a <code class="literal">HashMap</code>, but we don&#8217;t have to repeat the type parameters.</p> <blockquote><p>So now the reader is wondering: "Why's he using Int in one place and Integer in another?" </p> &#8212; TimMacEachern (2009-06-23 08:45:33) </blockquote> </div>urn:uuid:c917c30c-b0aa-3851-82ba-69a305a2b79eTimMacEachern2009-06-23T08:45:33-07:00Comment on 'Inferring Type Information' <div>Variable Declarations <p id="para_scala_also_requires_you_to_ini">Scala also requires you to initialize a <code class="literal">var</code> when it is declared. You can assign a new value to a <code class="literal">var</code> as often as you want. Again, to be precise, the <code class="literal">stockPrice</code> reference can be changed to point to a different <code class="literal">Double</code> object (<span class="emphasis"><em>e.g.</em></span>, <code class="literal">10.</code>). In this case, the object that <code class="literal">stockPrice</code> refers to can&#8217;t be changed, because <code class="literal">Doubles</code> in Scala are immutable.</p> <blockquote><p>Perhaps it would be better to say: "In this case, the value inherent in the object that stockPrice refers to can't be changed...". </p> &#8212; TimMacEachern (2009-06-23 08:28:08) </blockquote> </div>urn:uuid:099a8f55-fd8d-3a8a-92c3-ae6b88083d5fTimMacEachern2009-06-23T08:28:08-07:00Comment on 'Variable Declarations' <div>Option, Some, and None: Avoiding nulls <p id="para_we_also_show_the_alternative_m">We also show the alternative method, <code class="literal">getOrElse</code>, in the last two <code class="literal">println</code> statements. This method returns either the value in the <code class="literal">Option</code>, if it is a <code class="literal">Some</code> instance, or it returns the second argument we passed to <code class="literal">getOrElse</code>, if it is a <code class="literal">None</code> instance. In other words, the second argument to <code class="literal">getOrElse</code> functions as the default return value.</p> <blockquote><p>'the argument we passed to getOrElse'? Don't get the last sentence. </p> &#8212; robcd (2009-06-20 07:39:44) </blockquote> </div>urn:uuid:aba72042-5846-300d-8706-c715ae972abbrobcd2009-06-20T07:39:44-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Literals <p id="para_copy_and_past_these_strings_in">Copy and paste these strings into the <strong class="userinput"><code>scala</code></strong> interpreter. Do the same for the previous string examples. How are they interpreted differently?</p> <blockquote><p>Copy and past<em>e</em> </p> &#8212; robcd (2009-06-20 06:43:59) </blockquote> </div>urn:uuid:6798b08a-3644-31c2-9720-2a7cfa7a43d9robcd2009-06-20T06:43:59-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_script_return_error">Running this script returns the following error.</p> <blockquote><p>Did you copy-and-paste the previous example, by any chance? </p> &#8212; robcd (2009-06-20 06:10:11) </blockquote> </div>urn:uuid:c454a3e8-41e3-3045-98bf-19ef4b0b6bd7robcd2009-06-20T06:10:11-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para_multiple_arg_lists">We said &#8220;optional argument lists&#8221;, meaning more than one. Scala lets you define more than one argument list for a method. This is required for <span class="emphasis"><em>currying</em></span> methods, which we&#8217;ll discuss in <a class="xref" href="ch08.html#Currying" title="Currying">the section called &#8220;Currying&#8221;</a> in <a class="xref" href="ch08.html" title="Chapter&#160;8.&#160;Functional Programming in Scala">Chapter&#160;8, <i>Functional Programming in Scala</i></a>. It is also very useful for defining your own domain-specific languages (DSLs), as we&#8217;ll see in <a class="xref" href="ch11.html" title="Chapter&#160;11.&#160;Domain-Specific Languages in Scala">Chapter&#160;11, <i>Domain-Specific Languages in Scala</i></a>. Note that each argument list is surrounded by parentheses and the arguments are separated by commas.</p> <blockquote><p>"each argument is" =&gt; "each argument list is" </p> &#8212; bfollek (2009-06-19 09:16:27) </blockquote> </div>urn:uuid:4b88801f-bc65-3d64-925a-6e8fa2b4da60bfollek2009-06-19T09:16:27-07:00Comment on 'Method Declarations' <div>Variable Declarations <p id="para_scala_encourages_you_to_use_im">Scala encourages you to use immutable values whenever possible. As we will see, this promotes better object-oriented design and it is consistent with the principles of &#8220;pure&#8221; functional programming. It may take some getting used to, but you&#8217;ll find a newfound confidence in your code when it is written in an immutable style.</p> <blockquote><p>In the Notes that follows: </p> <p>"the referred to object is mutability" should be "the object referred to is mutable" </p> &#8212; bfollek (2009-06-19 09:10:24) </blockquote> </div>urn:uuid:2cfc6899-78bd-3ed3-9f40-4ae352a662bebfollek2009-06-19T09:10:24-07:00Comment on 'Variable Declarations' <div>Tuples <p id="para_running2">Running this script with <strong class="userinput"><code>scala</code></strong> produces the following output.</p> <blockquote><p>stay DRY: You can avoid repeating the "print the X item" statements using some cute loop like this </p> <p>(1 to tupleator.productArity).map((i:Int)=&gt;"Print item number "+i+": "+tupleator.productElement(i-1)).foreach(println(_)) </p> &#8212; javiervegeas (2009-06-14 14:33:57) </blockquote> </div>urn:uuid:43d6ef9a-6b79-3ebf-905a-0ebfaab53b4fjaviervegeas2009-06-14T14:33:57-07:00Comment on 'Tuples' <div>Organizing Code in Files and Namespaces <p id="para_scala_adopts_the_package_conce">Scala adopts the package concept that Java uses for namespaces, but Scala offers a more flexible syntax. Just as file names don&#8217;t have to match the type names, the package structure does not have to match the directory structure. So, you can define packages in files independent of their &#8220;physical&#8221; location.</p> <blockquote><p>... those files (declaring the same package) even dont have to reside in the same directory but could be spread all over the directory structure. </p> &#8212; magle (2009-06-10 01:24:03) </blockquote> </div>urn:uuid:bbe57fb5-8c25-3cfa-98a7-f7186e2c0402magle2009-06-10T01:24:03-07:00Comment on 'Organizing Code in Files and Namespaces' <div>Tuples <p id="para_tupleator">The <code class="literal">tupleator</code> method simply returns a &#8220;3-tuple&#8221; with the input arguments. The first statement that uses this method assigns the returned tuple to a single variable <code class="literal">t</code>. The next four statements print <code class="literal">t</code> in various ways. The first print statement calls <code class="literal">Tuple3.toString</code>, which wraps parentheses around the item list. The following three statements print each item in <code class="literal">t</code> separately. The expression <code class="literal">t._N</code> retrieves the <code class="literal">N</code> item, starting at 1, <span class="emphasis"><em>not</em></span> 0 (this choice follows functional programming conventions).</p> <blockquote><p>ups ... forget my last comment! </p> <p>Just saw, that it's already there :o) </p> &#8212; magle (2009-06-09 08:35:07) </blockquote> </div>urn:uuid:5d767994-909b-3fbc-b39b-486aade7d20dmagle2009-06-09T08:35:07-07:00Comment on 'Tuples' <div>Tuples <p id="para_tupleator">The <code class="literal">tupleator</code> method simply returns a &#8220;3-tuple&#8221; with the input arguments. The first statement that uses this method assigns the returned tuple to a single variable <code class="literal">t</code>. The next four statements print <code class="literal">t</code> in various ways. The first print statement calls <code class="literal">Tuple3.toString</code>, which wraps parentheses around the item list. The following three statements print each item in <code class="literal">t</code> separately. The expression <code class="literal">t._N</code> retrieves the <code class="literal">N</code> item, starting at 1, <span class="emphasis"><em>not</em></span> 0 (this choice follows functional programming conventions).</p> <blockquote><p>Maybe it's a bit early here (concerning Pattern), but using t._N isn't considered good style imho. You could instead use Pattern Matching when declaring your tuple variables like this: </p> <p>val (myFirstItem, mySecondItem, myThirdItem) = tupleator("Hello", 1, 2.3) </p> <p>Now you could refer to each item using the given identifier separately, like </p> <p>println("Print the first item: " + myFirstItem ) println("Print the second item: " + mySecondItem ) println("Print the third item: " + myThirdItem ) </p> &#8212; magle (2009-06-09 08:26:08) </blockquote> </div>urn:uuid:8f468512-db3c-3e59-aff4-5609cf9d0d13magle2009-06-09T08:26:08-07:00Comment on 'Tuples' <div>Option, Some, and None: Avoiding nulls <p id="para_most_languages_have_a_special_">Most languages have a special keyword or object that&#8217;s assigned to reference variables when there&#8217;s nothing else for them to refer to. In Java, this is <code class="literal">null</code>; in Ruby, it&#8217;s <code class="literal">nil</code>. In Java, <code class="literal">null</code> is a keyword, not an object, and thus it&#8217;s illegal to call any methods on it. But this is a confusing choice on the language designer&#8217;s part. Why return a keyword when the programmer expects an object?</p> <blockquote><p>Actually nil is an object in Ruby (instance of NilClass). </p> &#8212; Martin_Suesskraut (2009-06-09 03:38:59) </blockquote> </div>urn:uuid:cc2a290a-e819-329c-9ebf-9bb744c2e996Martin_Suesskraut2009-06-09T03:38:59-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Literals <p id="para_for_long_literals_it_is_nec">For <code class="literal">Long</code> literals, it is necessary to append the <code class="literal">L</code> or <code class="literal">l</code> character at the end of the literal. Otherwise, an <code class="literal">Int</code> is used. The valid values for an integer literal are bounded by the type of the variable to which the value will be assigned. <a class="xref" href="ch02.html#integer-boundaries-table" title="Table&#160;2.2.&#160;Ranges of allowed values for integer literals (boundaries are inclusive).">Table&#160;2.2, &#8220;Ranges of allowed values for integer literals (boundaries are inclusive).&#8221;</a> defines the limits, which are inclusive.</p> <blockquote><p>There is a dot within the last sentence (behind "inclusive)") </p> &#8212; Martin_Suesskraut (2009-06-09 01:49:09) </blockquote> </div>urn:uuid:20849524-0ebd-39ce-a4c8-f6bd34cf228bMartin_Suesskraut2009-06-09T01:49:09-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_what_happened_when_the_client">What happened? When the client was compiled, <code class="literal">StringUtil.toCollection</code> returned an <code class="literal">Array</code>. Then <code class="literal">toCollection</code> was changed to return <code class="literal">List</code>. In both versions, the method return value was inferred. Therefore, client should have been recompiled, too.</p> <blockquote><p>Shouldn't the comma between "scala" and "collection" be a dot? </p> &#8212; Martin_Suesskraut (2009-06-09 00:54:41) </blockquote> </div>urn:uuid:f768cbf6-795e-3ae8-8292-626146893222Martin_Suesskraut2009-06-09T00:54:41-07:00Comment on 'Inferring Type Information' <div>Abstract Types And Parameterized Types <p id="para_scala_supports_another_type_ab">Scala supports another type abstraction mechanism called <span class="emphasis"><em>abstract types</em></span>, used in many <span class="emphasis"><em>functional programming</em></span> languages, such as Haskell. Abstract types were also considered for inclusion in Java when generics were adopted. We want to introduce them now, because you&#8217;ll see many examples of them before we dive into their details 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>. For a very detailed comparison of these two mechanisms, see <a class="xref" href="apa.html#Bruce1998">[Bruce1998]</a>.</p> <blockquote><p>Right. Will fix. </p> &#8212; deanwampler (2009-06-07 18:09:09) </blockquote> </div>urn:uuid:4f38f83f-2bbd-3cb1-b284-aee6e83560aadeanwampler2009-06-07T18:09:09-07:00Comment on 'Abstract Types And Parameterized Types' <div>Option, Some, and None: Avoiding nulls <p id="para_the_convenient_gt_syntax_">The convenient <code class="literal">-&gt;</code> syntax for defining name-value pairs to initialize a <code class="literal">Map</code> will be discussed in <a class="xref" href="ch07.html#PredefObject" title="The Predef Object">the section called &#8220;The Predef Object&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a>. For now, we want to focus on the two groups of <code class="literal">println</code> statements, where we show what happens when you retrieve the values from the map. If you run this script with the <strong class="userinput"><code>scala</code></strong> command, you&#8217;ll get the following output.</p> <blockquote><p>Sorry. Now fixed. </p> &#8212; deanwampler (2009-06-07 18:08:47) </blockquote> </div>urn:uuid:7d13f194-748c-3d20-a2a0-44a1beb5e6a7deanwampler2009-06-07T18:08:47-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Option, Some, and None: Avoiding nulls <p id="para_to_be_more_consistent_with_the">To be more consistent with the goal of making everything an object, as well as to conform with functional programming conventions, Scala encourages you to use the <code class="literal">Option</code> type for variables and function return values when they may or may not refer to a value. When there is no value, use <code class="literal">None</code>, an <code class="literal">object</code> that is a subclass of <code class="literal">Option</code>. When there is a value, use <code class="literal">Some</code>, which wraps the value. <code class="literal">Some</code> is also a subclass of <code class="literal">Option</code>.</p> <blockquote><p>No problem. Thanks for going to the trouble... </p> &#8212; deanwampler (2009-06-07 18:08:34) </blockquote> </div>urn:uuid:937606c5-be02-343a-8568-52018e26502bdeanwampler2009-06-07T18:08:34-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Option, Some, and None: Avoiding nulls <p id="para_we_ll_discuss_the_standard_typ">We&#8217;ll discuss the standard type hierarchy for Scala in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a>. However, three useful classes to understand now are the <code class="literal">Option</code> class and its two subclasses, <code class="literal">Some</code> and <code class="literal">None</code>.</p> <blockquote><p>@Tobias, an unfortunate production error that has been fixed. Sorry. @Parth, we're working on putting numbers in the print version. </p> &#8212; deanwampler (2009-06-07 18:08:17) </blockquote> </div>urn:uuid:8bef0660-16b7-39c0-a0b5-614ff58cae0fdeanwampler2009-06-07T18:08:17-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Literals <p id="para_a_symbol_literal_is_a_single_q">A symbol literal is a single quote <code class="literal">'</code>, followed by a letter, followed by zero or more digits and letters. Note that an expression like <code class="literal">'1</code> is invalid, because the compiler thinks it is an incomplete character literal.</p> <blockquote><p>@Ariel. Right. Will clarify. @Parth. Will add a bit more on when they are typically used. </p> &#8212; deanwampler (2009-06-07 18:07:32) </blockquote> </div>urn:uuid:ecbb1f9a-f646-377f-bad1-7e22c660363edeanwampler2009-06-07T18:07:32-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_note_the_equals_sign_before_th">Note the equals sign before the body of <code class="literal">double</code>. Now, the output says we have defined <code class="literal">double</code> to return an <code class="literal">Int</code> and the second command does what we expect it to do.</p> <blockquote><p>Good point. Will add a paragraph for it. </p> &#8212; deanwampler (2009-06-07 18:05:55) </blockquote> </div>urn:uuid:3eb78b02-5a3c-3390-8284-9dfdf4010b1bdeanwampler2009-06-07T18:05:55-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_why_did_the_second_command_pri">Why did the second command print <code class="literal">()</code> instead of <code class="literal">4</code>? Look carefully at what the <strong class="userinput"><code>scala</code></strong> interpreter said the first command returned, <code class="literal">double (Int)Unit</code>. We defined a method named <code class="literal">double</code> that takes an <code class="literal">Int</code> argument and returns <code class="literal">Unit</code>. The method doesn&#8217;t return an <code class="literal">Int</code> as we would expect.</p> <blockquote><p>Bug. will fix. Thanks! </p> &#8212; deanwampler (2009-06-07 18:04:27) </blockquote> </div>urn:uuid:2d45e11e-7954-3802-9dac-ec6576cea215deanwampler2009-06-07T18:04:27-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_if_you_compile_these_files_yo">If you compile these files with <strong class="userinput"><code>scala</code></strong>, you can run the client as follows.</p> <blockquote><p>Reworking the example to remove this use of tuples. </p> &#8212; deanwampler (2009-06-07 18:04:07) </blockquote> </div>urn:uuid:5a687d58-b5ca-3799-9831-6f02e565bbc1deanwampler2009-06-07T18:04:07-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_functional_languages_like_hask">Pure functional languages like Haskell (see, <span class="emphasis"><em>e.g.,</em></span> <a class="xref" href="apa.html#OSullivan2009">[O'Sullivan2009]</a>) use type inference algorithms like <span class="emphasis"><em>Hindley-Milner</em></span> (see <a class="xref" href="apa.html#Spiewak2008">[Spiewak2008]</a> for an easily digested explanation). Code written in these languages require type annotations less often than in Scala, because Scala&#8217;s type inference algorithm has to support object-oriented typing as well as functional typing. So, Scala requires more type annotations than languages like Haskell. Here is a summary of the rules for when explicit type annotations are required in Scala.</p> <blockquote><p>Rewording </p> &#8212; deanwampler (2009-06-07 18:03:37) </blockquote> </div>urn:uuid:10365cb8-222d-3942-822e-8312d01b3081deanwampler2009-06-07T18:03:37-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_pairsstringtomap_method_">The <code class="literal">toCollection</code> method splits a string on spaces and returns an <code class="literal">Array</code> containing the substrings. The return type is inferred, which is a potential problem, as we will see. The method is somewhat contrived, but it will illustrate our point. Here is a client of <code class="literal">StringUtil</code> that uses this method.</p> <blockquote><p>I came up with a much simpler example. </p> &#8212; deanwampler (2009-06-04 14:40:54) </blockquote> </div>urn:uuid:47bc87cc-f21b-3cfc-b704-9d59e70a67d3deanwampler2009-06-04T14:40:54-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_pairsstringtomap_method_">The <code class="literal">toCollection</code> method splits a string on spaces and returns an <code class="literal">Array</code> containing the substrings. The return type is inferred, which is a potential problem, as we will see. The method is somewhat contrived, but it will illustrate our point. Here is a client of <code class="literal">StringUtil</code> that uses this method.</p> <blockquote><p>I'm considering a different example that doesn't require these features that haven't been discussed yet. Concerning numbers and labels, we already do that with some of the larger examples. We are evaluating the balance between too many labeled examples and too few. </p> &#8212; deanwampler (2009-06-04 14:15:06) </blockquote> </div>urn:uuid:f74e359d-668c-3d43-96c3-1ef58509149bdeanwampler2009-06-04T14:15:06-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_running_this_script_gives_you_">Running this script gives you the following error.</p> <blockquote><p>Doh! Thanks. </p> &#8212; deanwampler (2009-06-04 11:44:11) </blockquote> </div>urn:uuid:61d0ab15-57aa-3a58-a114-0314de21c060deanwampler2009-06-04T11:44:11-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_method_return_value_cases">Let&#8217;s look at examples where explicit declarations of method return types are required. In the following script, the <code class="literal">upCase</code> method has a conditional return statement for zero-length strings.</p> <blockquote><p>Actually, you aren't required to declare the return type. for example, implicit def int2String(i:Int) = i.toString works fine. However, it's actually a good idea to add the return type annotation anyway to avoid some potential subtle bugs when the method is refined over time. </p> &#8212; deanwampler (2009-06-04 11:42:18) </blockquote> </div>urn:uuid:1ed854b4-22f5-338d-a4d1-244ad092e768deanwampler2009-06-04T11:42:18-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_recall_from_chapter_1_that_sca">Recall from Chapter 1 that Scala uses square brackets (<code class="literal">[&#8230;]</code>) for generic type parameters. We specify <code class="literal">Map[Integer, String]</code> on the left-hand side of the equals sign. (We are sticking with Java types for the example.) On the right-hand side, we instantiate the actual type we want, a <code class="literal">HashMap</code>, but we don&#8217;t have to repeat the type parameters.</p> <blockquote><p>Thanks. will fix. </p> &#8212; deanwampler (2009-06-04 11:33:54) </blockquote> </div>urn:uuid:293dd508-275b-39e3-aa6a-036d1ae8aa30deanwampler2009-06-04T11:33:54-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_scala_supports__type_inferenci">Scala supports <span class="emphasis"><em>type inference</em></span> (see, for example, <a class="xref" href="apa.html#TypeInference">[TypeInference]</a> and <a class="xref" href="apa.html#Pierce2002">[Pierce2002]</a>). The language&#8217;s compiler can discern quite a bit of type information from the context, without explicit type annotations. Here&#8217;s the same declaration rewritten in Scala, with inferred type information.</p> <blockquote><p>We've been using the term parser, but yes, it's the compiler. Plan to fix. </p> &#8212; deanwampler (2009-06-04 11:31:08) </blockquote> </div>urn:uuid:84d253c3-78bf-3221-9d2a-9cdcd5f1b0e4deanwampler2009-06-04T11:31:08-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para_the_second_method_is_nested_in">The second method calls itself recursively, passing an <code class="literal">accumulator</code> parameter, where the result of the calculation is &#8220;accumulated&#8221;. Note that we return the accumulated value when the counter <code class="literal">i</code> reaches 1. (We&#8217;re ignoring invalid negative integers. The function actually returns 1 for <code class="literal">i &lt; 0</code>.) After the definition of the nested method, <code class="literal">factorial</code> calls it with the passed-in value <code class="literal">i</code> and the initial accumulator value of 1.</p> <blockquote><p>Good point. Expanded this section to discuss it. </p> &#8212; deanwampler (2009-06-04 11:29:54) </blockquote> </div>urn:uuid:3046b327-b10b-37f0-b45d-d2e43e8f67e1deanwampler2009-06-04T11:29:54-07:00Comment on 'Method Declarations' <div>Method Declarations <p id="para_if_a_method_body_has_more_than">If a method body has more than one expression, you must surround it with curly braces <code class="literal">{&#8230;}</code>. You can omit the braces if the method body has just one expression.</p> <blockquote><p>Yes, ti should be expression. Thanks. </p> &#8212; deanwampler (2009-06-03 14:36:35) </blockquote> </div>urn:uuid:f96d6cd1-c246-3c33-b7db-fa40168d0dc5deanwampler2009-06-03T14:36:35-07:00Comment on 'Method Declarations' <div>Method Declarations <p id="para_method_declarations">We saw several examples in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a> of how to define <span class="emphasis"><em>methods</em></span>, which are functions that are members of a class. Method <span class="emphasis"><em>definitions</em></span> start with the <code class="literal">def</code> keyword, followed by optional argument lists, a colon character &#8216;:&#8217; and the return type of the method, an equals sign &#8216;=&#8217;, and finally the method body. Methods are implicitly <span class="emphasis"><em>declared</em></span> &#8220;abstract&#8221; if you leave off the equals sign and method body. The enclosing type is then itself abstract. We&#8217;ll discuss abstract types in more detail in <a class="xref" href="ch05.html" title="Chapter&#160;5.&#160;Basic Object-Oriented Programming in Scala">Chapter&#160;5, <i>Basic Object-Oriented Programming in Scala</i></a>.</p> <blockquote><p>Will fix. </p> &#8212; deanwampler (2009-06-10 05:44:16) </blockquote> </div>urn:uuid:5ede91cf-2fb8-3a9a-ad66-41d5a46b5877deanwampler2009-06-10T05:44:16-07:00Comment on 'Method Declarations' <div>Variable Declarations <p id="para_scala_allows_you_to_decide_whe">Scala allows you to decide whether a variable is immutable (read-only) or not (read-write) when you declare it. An immutable &#8220;variable&#8221; is declared with the keyword <code class="literal">val</code> (think <span class="emphasis"><em>value object</em></span>).</p> <blockquote><p>@Reid, I think the most recent drafts use "inference" (my bad...) </p> <p>@Marek. Yes, it should be more clear. I'll fix. </p> &#8212; deanwampler (2009-06-03 12:38:14) </blockquote> </div>urn:uuid:eb3bff05-cd8b-39f1-a1de-29073eafdc54deanwampler2009-06-03T12:38:14-07:00Comment on 'Variable Declarations' <div>Semicolons <p id="para_you_may_have_already_noticed_t">You may have already noticed that there were very few semicolons in the code examples in the previous chapter. You can use semicolons to separate statements and expressions, as in Java, C, PHP, and similar languages. In most cases, though, Scala behaves like many scripting languages in treating the end of the line as the end of a statement or an expression. When a statement or expression is too long for one line, Scala can usually infer when you are continuing on to the next line, as shown in this example.</p> <blockquote><p>We didn't say it explicitly, but yes, anything optional tends to get left out ;) I'm not sure why the JS community prefers to have the semicolons after statements. </p> &#8212; deanwampler (2009-06-03 12:37:07) </blockquote> </div>urn:uuid:bd8efa14-b3f6-3b42-a546-f78ae98b5c13deanwampler2009-06-03T12:37:07-07:00Comment on 'Semicolons' <div>Option, Some, and None: Avoiding nulls <p id="para_to_be_more_consistent_with_the">To be more consistent with the goal of making everything an object, as well as to conform with functional programming conventions, Scala encourages you to use the <code class="literal">Option</code> type for variables and function return values when they may or may not refer to a value. When there is no value, use <code class="literal">None</code>, an <code class="literal">object</code> that is a subclass of <code class="literal">Option</code>. When there is a value, use <code class="literal">Some</code>, which wraps the value. <code class="literal">Some</code> is also a subclass of <code class="literal">Option</code>.</p> <blockquote><p>Oops. Sorry ... the above comment from me was premature. </p> &#8212; parthm (2009-06-01 22:23:20) </blockquote> </div>urn:uuid:6fbeed37-c09c-3206-95b5-ba33e7bdd1a7parthm2009-06-01T22:23:20-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Option, Some, and None: Avoiding nulls <p id="para_to_be_more_consistent_with_the">To be more consistent with the goal of making everything an object, as well as to conform with functional programming conventions, Scala encourages you to use the <code class="literal">Option</code> type for variables and function return values when they may or may not refer to a value. When there is no value, use <code class="literal">None</code>, an <code class="literal">object</code> that is a subclass of <code class="literal">Option</code>. When there is a value, use <code class="literal">Some</code>, which wraps the value. <code class="literal">Some</code> is also a subclass of <code class="literal">Option</code>.</p> <blockquote><p>It may be good to have a small example here. This kind of approach is used only in some languages like Haskell (type Maybe) and showing example code will help understand the use better. </p> <p>scala&gt; var x: Option[Int] = Some(1) x: Option[Int] = Some(1) </p> <p>scala&gt; var y: Option[Int] = None <br /> y: Option[Int] = None </p> <p>scala&gt; def foo(z: Option[Int]) = { | z match { <br /> | case Some(v) =&gt; v <br /> | case None =&gt; -1 <br /> | } | } foo: (Option[Int])Int </p> <p>scala&gt; foo(x) res10: Int = 1 </p> <p>scala&gt; foo(y) res11: Int = -1 </p> &#8212; parthm (2009-06-01 22:21:26) </blockquote> </div>urn:uuid:d6f09378-cfdd-3eda-970f-63d59c8e0264parthm2009-06-01T22:21:26-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Option, Some, and None: Avoiding nulls <p id="para_we_ll_discuss_the_standard_typ">We&#8217;ll discuss the standard type hierarchy for Scala in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a>. However, three useful classes to understand now are the <code class="literal">Option</code> class and its two subclasses, <code class="literal">Some</code> and <code class="literal">None</code>.</p> <blockquote><p>The chapter/section number might be useful for "The Scala Type Hierarchy". Especially from the print copy. </p> &#8212; parthm (2009-06-01 22:14:02) </blockquote> </div>urn:uuid:fe9565e8-043a-39e0-a71b-98a9350f0162parthm2009-06-01T22:14:02-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Literals <p id="para_a_symbol_literal_is_a_single_q">A symbol literal is a single quote <code class="literal">'</code>, followed by a letter, followed by zero or more digits and letters. Note that an expression like <code class="literal">'1</code> is invalid, because the compiler thinks it is an incomplete character literal.</p> <blockquote><p>It may be good to mention what Symbols are used for in Scala. Coming form Lisp I understand Symbols (assuming its the same in Scala) but cant really understand their use in Scala. If this is discussed in a later chapter/section maybe a pointer to it can be put. </p> &#8212; parthm (2009-06-01 21:53:28) </blockquote> </div>urn:uuid:d568e75c-d976-3c04-90b7-729c9aaf9699parthm2009-06-01T21:53:28-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_the_pairsstringtomap_method_">The <code class="literal">toCollection</code> method splits a string on spaces and returns an <code class="literal">Array</code> containing the substrings. The return type is inferred, which is a potential problem, as we will see. The method is somewhat contrived, but it will illustrate our point. Here is a client of <code class="literal">StringUtil</code> that uses this method.</p> <blockquote><p>It would be good to mention that the (string).r notation used above is the regex notation and creates a scala.util.matching.Regex </p> &#8212; parthm (2009-06-01 21:21:59) </blockquote> </div>urn:uuid:eea324d2-a0f8-3ffb-86c0-a858a6af587fparthm2009-06-01T21:21:59-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_if_you_compile_these_files_yo">If you compile these files with <strong class="userinput"><code>scala</code></strong>, you can run the client as follows.</p> <blockquote><p>It would be good to have a short note on the kv._1, kv._2 notation used above for people don't know about accessing scala tuples </p> &#8212; parthm (2009-06-01 21:19:39) </blockquote> </div>urn:uuid:2d07f2fb-6404-39f3-bde8-551dbf23aba8parthm2009-06-01T21:19:39-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_the_pairsstringtomap_method_">The <code class="literal">toCollection</code> method splits a string on spaces and returns an <code class="literal">Array</code> containing the substrings. The return type is inferred, which is a potential problem, as we will see. The method is somewhat contrived, but it will illustrate our point. Here is a client of <code class="literal">StringUtil</code> that uses this method.</p> <blockquote><p>As Lists haven't been discussed in some detail at this point, 'head :: tail' pattern matching and Nil usage in the above example may be difficult to understand for someone who doesn't know about it already. Maybe a short note on that can be added. </p> <p>Also, is addition numbers to code listings (Listing. N) planned? </p> &#8212; parthm (2009-06-01 21:15:24) </blockquote> </div>urn:uuid:e5ec3188-3ff2-32b3-9d6b-f3dc15a08520parthm2009-06-01T21:15:24-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_running_this_script_gives_you_">Running this script gives you the following error.</p> <blockquote><p>In the printlns, upper should be upCase </p> &#8212; parthm (2009-06-01 21:02:37) </blockquote> </div>urn:uuid:c7a7ec63-995a-3695-ad1a-8f62031d5904parthm2009-06-01T21:02:37-07:00Comment on 'Inferring Type Information' <div>Inferring Type Information <p id="para_recall_from_chapter_1_that_sca">Recall from Chapter 1 that Scala uses square brackets (<code class="literal">[&#8230;]</code>) for generic type parameters. We specify <code class="literal">Map[Integer, String]</code> on the left-hand side of the equals sign. (We are sticking with Java types for the example.) On the right-hand side, we instantiate the actual type we want, a <code class="literal">HashMap</code>, but we don&#8217;t have to repeat the type parameters.</p> <blockquote><blockquote><p>On the first line we specify Map[Int, String] </p> </blockquote><p>You probably mean Map[Integer, String] above. </p> &#8212; parthm (2009-06-01 21:01:26) </blockquote> </div>urn:uuid:8ceb5c67-7db4-319b-b407-9198b40e9e9bparthm2009-06-01T21:01:26-07:00Comment on 'Inferring Type Information' <div>Abstract Types And Parameterized Types <p id="para_scala_supports_another_type_ab">Scala supports another type abstraction mechanism called <span class="emphasis"><em>abstract types</em></span>, used in many <span class="emphasis"><em>functional programming</em></span> languages, such as Haskell. Abstract types were also considered for inclusion in Java when generics were adopted. We want to introduce them now, because you&#8217;ll see many examples of them before we dive into their details 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>. For a very detailed comparison of these two mechanisms, see <a class="xref" href="apa.html#Bruce1998">[Bruce1998]</a>.</p> <blockquote><p>Generics were added to Java in version 5.0, which was released in 2004, not in the late 90's. </p> &#8212; chifazo (2009-05-30 14:36:39) </blockquote> </div>urn:uuid:846ad9c8-4957-3776-9dc5-5adcd44f9752chifazo2009-05-30T14:36:39-07:00Comment on 'Abstract Types And Parameterized Types' <div>Literals <p id="para_a_symbol_literal_is_a_single_q">A symbol literal is a single quote <code class="literal">'</code>, followed by a letter, followed by zero or more digits and letters. Note that an expression like <code class="literal">'1</code> is invalid, because the compiler thinks it is an incomplete character literal.</p> <blockquote><p>Instead of saying: "because the parser thinks it is an incomplete integer literal", I think it should say: "because the parser thinks it is an incomplete character literal" </p> &#8212; chifazo (2009-05-30 13:38:31) </blockquote> </div>urn:uuid:870f9cf6-8e3e-3625-8e23-1b17fd5e4111chifazo2009-05-30T13:38:31-07:00Comment on 'Literals' <div>Literals <p id="para_floating_point_literals_are_ex">Floating point literals are expressions with zero or more digits, followed by a period <code class="literal">.</code>, followed by zero or more digits. If there are no digits before the period, <span class="emphasis"><em>i.e.,</em></span> the number is less than 1.0, then there must be one or more digits after the period. For <code class="literal">Float</code> literals, append the <code class="literal">F</code> or <code class="literal">f</code> character at the end of the literal. Otherwise, a <code class="literal">Double</code> is assumed. You can optionally append a <code class="literal">D</code> or <code class="literal">d</code> for a <code class="literal">Double</code>.</p> <blockquote><p>According to this definition, a single period "." would be a valid floating point literal. There must be at least one digit before or after the period. </p> &#8212; chifazo (2009-05-30 13:28:01) </blockquote> </div>urn:uuid:87dcb15a-f7c9-380e-8390-f5a999b5d1f5chifazo2009-05-30T13:28:01-07:00Comment on 'Literals' <div>Inferring Type Information <p id="para_note_the_equals_sign_before_th">Note the equals sign before the body of <code class="literal">double</code>. Now, the output says we have defined <code class="literal">double</code> to return an <code class="literal">Int</code> and the second command does what we expect it to do.</p> <blockquote><p>You may could mention the reason way methods without an equals sign always leads to return type Unit: Methods offering an equals sign are considered as functions which always return a value when called. Methods without an equals sign are considered as procedures which (only) produce some side effects. That said, those methods don't return any value and hence offer a return type Unit. </p> &#8212; magle (2009-05-29 06:43:08) </blockquote> </div>urn:uuid:3693e48f-7edc-379d-9ce4-c7b5b86ce87emagle2009-05-29T06:43:08-07:00Comment on 'Inferring Type Information' <div>Method Declarations <p id="para_the_second_method_is_nested_in">The second method calls itself recursively, passing an <code class="literal">accumulator</code> parameter, where the result of the calculation is &#8220;accumulated&#8221;. Note that we return the accumulated value when the counter <code class="literal">i</code> reaches 1. (We&#8217;re ignoring invalid negative integers. The function actually returns 1 for <code class="literal">i &lt; 0</code>.) After the definition of the nested method, <code class="literal">factorial</code> calls it with the passed-in value <code class="literal">i</code> and the initial accumulator value of 1.</p> <blockquote><p>The nested method declares the same parameter name 'i' as the surrounding method, thus shadowing the parameter of the surrounding method. I'm not sure if this is done in full purpose here, since you're otherwise able to refer to the parameterlist of the surrounding method within the body of the nested method. May be better to use a different parameter name for the nested method even for the sake of clarity. </p> &#8212; magle (2009-05-26 02:58:35) </blockquote> </div>urn:uuid:2be9fed7-f73d-3adf-aa13-9b1e4a7a08bcmagle2009-05-26T02:58:35-07:00Comment on 'Method Declarations' <div>Inferring Type Information <p id="para_why_did_the_second_command_pri">Why did the second command print <code class="literal">()</code> instead of <code class="literal">4</code>? Look carefully at what the <strong class="userinput"><code>scala</code></strong> interpreter said the first command returned, <code class="literal">double (Int)Unit</code>. We defined a method named <code class="literal">double</code> that takes an <code class="literal">Int</code> argument and returns <code class="literal">Unit</code>. The method doesn&#8217;t return an <code class="literal">Int</code> as we would expect.</p> <blockquote><p>Wouldn't be "square" a more appropriate method name? Or def double(i: Int) = { 2 * i }<br /> </p> &#8212; battisti (2009-05-25 11:06:09) </blockquote> </div>urn:uuid:4094d5f0-b711-3060-bf32-dadb9a1c2d10battisti2009-05-25T11:06:09-07:00Comment on 'Inferring Type Information' <div>Variable Declarations <p id="para_scala_allows_you_to_decide_whe">Scala allows you to decide whether a variable is immutable (read-only) or not (read-write) when you declare it. An immutable &#8220;variable&#8221; is declared with the keyword <code class="literal">val</code> (think <span class="emphasis"><em>value object</em></span>).</p> <blockquote><p><code>val</code> only means that a variable cannot be re-assigned, therefore <code>val a = new Array[Int](3)</code> is still a mutable variable. I think the paragraph above should explain that it is the <em>name</em> that cannot be reassigned, not that the object bound to it becomes read-only in some magic way. </p> &#8212; Leonidas (2009-05-24 15:41:20) </blockquote> </div>urn:uuid:007c6ebc-599f-336e-a65f-b50417f2212fLeonidas2009-05-24T15:41:20-07:00Comment on 'Variable Declarations' <div>Semicolons <p id="para_you_may_have_already_noticed_t">You may have already noticed that there were very few semicolons in the code examples in the previous chapter. You can use semicolons to separate statements and expressions, as in Java, C, PHP, and similar languages. In most cases, though, Scala behaves like many scripting languages in treating the end of the line as the end of a statement or an expression. When a statement or expression is too long for one line, Scala can usually infer when you are continuing on to the next line, as shown in this example.</p> <blockquote><p>So, the recommendation for Scala code is to leave the semicolons out, right? I'm asking because semicolons are can be included or left out in both Python and JavaScript, but in Python it is considered a bad habit to put them in and in JavaScript, it is good style to finish the statements with semicolons. </p> <p>I want to write idiomatic Scala-code from day one, so it would be good to know a recommendation in this regard. </p> &#8212; Leonidas (2009-05-24 15:32:01) </blockquote> </div>urn:uuid:681e4f21-40da-3369-a6f5-4a924e28426bLeonidas2009-05-24T15:32:01-07:00Comment on 'Semicolons' <div>Method Declarations <p id="para_if_a_method_body_has_more_than">If a method body has more than one expression, you must surround it with curly braces <code class="literal">{&#8230;}</code>. You can omit the braces if the method body has just one expression.</p> <blockquote><p>Shouldn't it be "has more than one expression" instead of "more than one statement"? According to the Scala Reference: "Both declarations and definitions produce bindings that associate type names with type definitions or bounds..." to associate something with something, you'd need an expression (i.e. something, that returns a value with a type to bind against) and not a statement which "just does something" </p> &#8212; battisti (2009-05-24 05:55:07) </blockquote> </div>urn:uuid:2443bd91-0db4-3597-8384-929fbdfbb678battisti2009-05-24T05:55:07-07:00Comment on 'Method Declarations' <div>Inferring Type Information <p id="para_functional_languages_like_hask">Pure functional languages like Haskell (see, <span class="emphasis"><em>e.g.,</em></span> <a class="xref" href="apa.html#OSullivan2009">[O'Sullivan2009]</a>) use type inference algorithms like <span class="emphasis"><em>Hindley-Milner</em></span> (see <a class="xref" href="apa.html#Spiewak2008">[Spiewak2008]</a> for an easily digested explanation). Code written in these languages require type annotations less often than in Scala, because Scala&#8217;s type inference algorithm has to support object-oriented typing as well as functional typing. So, Scala requires more type annotations than languages like Haskell. Here is a summary of the rules for when explicit type annotations are required in Scala.</p> <blockquote><p>"are not as mature" sounds like there's hope for a lot of improvement, which is not at all that clear to me. </p> &#8212; clojure (2009-05-21 03:47:34) </blockquote> </div>urn:uuid:9d67153e-f5a7-3007-a596-3787d4b3773eclojure2009-05-21T03:47:34-07:00Comment on 'Inferring Type Information' <div>Variable Declarations <p id="para_scala_allows_you_to_decide_whe">Scala allows you to decide whether a variable is immutable (read-only) or not (read-write) when you declare it. An immutable &#8220;variable&#8221; is declared with the keyword <code class="literal">val</code> (think <span class="emphasis"><em>value object</em></span>).</p> <blockquote><p>type inferencer just doesn't sound right, perhaps it should be "Type inference can figure this one out easily." </p> &#8212; reidhoch (2009-05-20 05:28:41) </blockquote> </div>urn:uuid:4decd94d-1674-3795-9fba-379a3cd03b39reidhoch2009-05-20T05:28:41-07:00Comment on 'Variable Declarations' <div>Inferring Type Information <p id="para_method_return_value_cases">Let&#8217;s look at examples where explicit declarations of method return types are required. In the following script, the <code class="literal">upCase</code> method has a conditional return statement for zero-length strings.</p> <blockquote><p>3.d When the method is an implicit method that the compiler will automatically use to convert from one type to another. </p> &#8212; retronym (2009-05-20 04:01:04) </blockquote> </div>urn:uuid:19933018-4e08-3769-b431-2c4683eb5117retronym2009-05-20T04:01:04-07:00Comment on 'Inferring Type Information' <div>Option, Some, and None: Avoiding nulls <p id="para_the_convenient_gt_syntax_">The convenient <code class="literal">-&gt;</code> syntax for defining name-value pairs to initialize a <code class="literal">Map</code> will be discussed in <a class="xref" href="ch07.html#PredefObject" title="The Predef Object">the section called &#8220;The Predef Object&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a>. For now, we want to focus on the two groups of <code class="literal">println</code> statements, where we show what happens when you retrieve the values from the map. If you run this script with the <strong class="userinput"><code>scala</code></strong> command, you&#8217;ll get the following output.</p> <blockquote><p>Again two times "in" </p> &#8212; myrddin (2009-05-20 04:00:26) </blockquote> </div>urn:uuid:bbc0385b-5e94-3a64-a1f3-9a177d7ebce6myrddin2009-05-20T04:00:26-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Method Declarations <p id="para_method_declarations">We saw several examples in <a class="xref" href="ch01.html" title="Chapter&#160;1.&#160;Zero to Sixty: Introducing Scala">Chapter&#160;1, <i>Zero to Sixty: Introducing Scala</i></a> of how to define <span class="emphasis"><em>methods</em></span>, which are functions that are members of a class. Method <span class="emphasis"><em>definitions</em></span> start with the <code class="literal">def</code> keyword, followed by optional argument lists, a colon character &#8216;:&#8217; and the return type of the method, an equals sign &#8216;=&#8217;, and finally the method body. Methods are implicitly <span class="emphasis"><em>declared</em></span> &#8220;abstract&#8221; if you leave off the equals sign and method body. The enclosing type is then itself abstract. We&#8217;ll discuss abstract types in more detail in <a class="xref" href="ch05.html" title="Chapter&#160;5.&#160;Basic Object-Oriented Programming in Scala">Chapter&#160;5, <i>Basic Object-Oriented Programming in Scala</i></a>.</p> <blockquote><p>'followed by an optional argument list in parenthesese' should be qualified with a footnote that refers to a later section showing methods with multiple argument lists and currying. </p> &#8212; retronym (2009-06-10 05:44:16) </blockquote> </div>urn:uuid:f1eb8ac1-0f98-3aeb-a478-211e60a269a9retronym2009-06-10T05:44:16-07:00Comment on 'Method Declarations' <div>Option, Some, and None: Avoiding nulls <p id="para_we_ll_discuss_the_standard_typ">We&#8217;ll discuss the standard type hierarchy for Scala in <a class="xref" href="ch07.html#ScalaTypeHierarchy" title="The Scala Type Hierarchy">the section called &#8220;The Scala Type Hierarchy&#8221;</a> in <a class="xref" href="ch07.html" title="Chapter&#160;7.&#160;The Scala Object System">Chapter&#160;7, <i>The Scala Object System</i></a>. However, three useful classes to understand now are the <code class="literal">Option</code> class and its two subclasses, <code class="literal">Some</code> and <code class="literal">None</code>.</p> <blockquote><p>Too times "in" at the end of the first sentence without the right object. </p> &#8212; myrddin (2009-05-20 03:50:42) </blockquote> </div>urn:uuid:2b513a77-0818-306a-b123-6728a591ccf7myrddin2009-05-20T03:50:42-07:00Comment on 'Option, Some, and None: Avoiding nulls' <div>Inferring Type Information <p id="para_scala_supports__type_inferenci">Scala supports <span class="emphasis"><em>type inference</em></span> (see, for example, <a class="xref" href="apa.html#TypeInference">[TypeInference]</a> and <a class="xref" href="apa.html#Pierce2002">[Pierce2002]</a>). The language&#8217;s compiler can discern quite a bit of type information from the context, without explicit type annotations. Here&#8217;s the same declaration rewritten in Scala, with inferred type information.</p> <blockquote><p>Actually it's the language's <em>compiler</em> that can discern quite a bit of type information. </p> &#8212; zefhemel (2009-05-20 03:00:35) </blockquote> </div>urn:uuid:a472995d-4f5d-36fb-82ae-bef59b6a6fbazefhemel2009-05-20T03:00:35-07:00Comment on 'Inferring Type Information'