Comments on Programming Scalaurn:uuid:e1ad59ac-7c5e-3c42-ba7d-ed904f8eb72c2009-08-11T20:58:59-07:00 <div>Value Types <p id="para_the_attempt_method_will_eval">The <code class="literal">attempt</code> method will evaluate the <span class="emphasis"><em>call-by-value</em></span> parameter <code class="literal">operation</code> and return its <code class="literal">Boolean</code> result, wrapped in a <code class="literal">Right</code> <span class="emphasis"><em>or</em></span> any <code class="literal">Throwable</code> that is caught, wrapped in a <code class="literal">Left</code>. The script produces this output.</p> <blockquote><p>the "call-by-name" parameter </p> &#8212; Dirk_K (2009-07-21 02:09:43) </blockquote> </div>urn:uuid:b32c3490-1691-3af6-b9e1-a17814c5184aDirk_K2009-07-21T02:09:43-07:00Comment on 'Value Types' <div>Variance Under Inheritance <p id="para_in_line_6_we_pass_in_a_fun">Finally, we come to the point of this example. In line #6, we pass in a function that is &#8220;willing&#8221; to accept a <code class="literal">CSuper</code> and &#8220;promises&#8221; to return a <code class="literal">CSub</code>. That is, this function is type inferred to be <code class="literal">Function1[CSuper,CSub]</code>. In effect, it widens the allowed instances by accepting a supertype. Keep in mind that it will never actually be passed a <code class="literal">CSuper</code> by <code class="literal">useF</code>, only a <code class="literal">C</code>. However, since it can accept a wider set of instances, it will work fine if it only gets <code class="literal">C</code> instances.</p> <blockquote><p>will fix. Thanks! </p> &#8212; deanwampler (2009-07-12 22:06:26) </blockquote> </div>urn:uuid:bedf0ca9-b2b3-3c1e-92a7-733771592ee0deanwampler2009-07-12T22:06:26-07:00Comment on 'Variance Under Inheritance' <div>Variance Under Inheritance <p id="para_in_line_6_we_pass_in_a_fun">Finally, we come to the point of this example. In line #6, we pass in a function that is &#8220;willing&#8221; to accept a <code class="literal">CSuper</code> and &#8220;promises&#8221; to return a <code class="literal">CSub</code>. That is, this function is type inferred to be <code class="literal">Function1[CSuper,CSub]</code>. In effect, it widens the allowed instances by accepting a supertype. Keep in mind that it will never actually be passed a <code class="literal">CSuper</code> by <code class="literal">useF</code>, only a <code class="literal">C</code>. However, since it can accept a wider set of instances, it will work fine if it only gets <code class="literal">C</code> instances.</p> <blockquote><p>I think you mean to say, 'In line #6' instead of 'In line #5'. </p> &#8212; sixblackbeans (2009-07-10 14:19:05) </blockquote> </div>urn:uuid:f68a0519-50e0-33b6-9ef5-35dcebcacc54sixblackbeans2009-07-10T14:19:05-07:00Comment on 'Variance Under Inheritance' <div>Chapter 12. The Scala Type System <p id="para_types_intro">Scala is a statically-typed language. Its type system is one of the most sophisticated in any programming language, in part because it combines comprehensive ideas from functional programming and object-oriented programming. The type system tries to be logically comprehensive, complete, and consistent. It exceeds limitations in Java&#8217;s type system while containing innovations which appear in Scala for the first time.</p> <blockquote><p>Fixed the "its". Will consider the 2nd comment. thx. </p> &#8212; deanwampler (2009-07-01 15:12:11) </blockquote> </div>urn:uuid:5447749a-0a30-3abe-b9f9-0747a211f1b0deanwampler2009-07-01T15:12:11-07:00Comment on 'Chapter 12. The Scala Type System' <div>Type Bounds <p id="para_you_would_use_the_underscore_">The underscore &#8216;_&#8217; is used for <span class="emphasis"><em>existential types</em></span>, which we&#8217;ll cover below, in <a class="xref" href="ch12.html#ExistentialTypes" title="Existential Types">the section called &#8220;Existential Types&#8221;</a>.</p> <blockquote><p>I'm pretty sure you're right; the underscore only applies to existential types. will fix. </p> &#8212; deanwampler (2009-07-01 14:27:33) </blockquote> </div>urn:uuid:2ff759d3-6325-3f11-9680-901c78bfa287deanwampler2009-07-01T14:27:33-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_a__view__is_a_value_of_functio">A <span class="emphasis"><em>view</em></span> is an implicit value of function type that converts a type <code class="literal">A</code> to <code class="literal">B</code>. The function has the type <code class="literal">A =&gt; B</code> or <code class="literal">(=&gt; A) =&gt; B</code> (recall that <code class="literal">(=&gt; A)</code> is a <span class="emphasis"><em>by-name parameter</em></span>). An in-scope implicit method with the same signature can also be used as a view, <span class="emphasis"><em>e.g.,</em></span> an implicit method imported from an <code class="literal">object</code>. The term <span class="emphasis"><em>view</em></span> conveys the sense of having a view from one type (<code class="literal">A</code>) to another type (<code class="literal">B</code>).</p> <blockquote><p>Thanks. Forgot to mention the implicit keyword (for both in-scope methods and function values), etc. </p> &#8212; deanwampler (2009-07-01 13:07:39) </blockquote> </div>urn:uuid:bcca2524-5b3d-3f4f-aa0b-6d3f00c9025bdeanwampler2009-07-01T13:07:39-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_the_new_list_of_type_list_any">The new list of type <code class="literal">List[Any]</code>, since <code class="literal">Any</code> is the closest common supertype of <code class="literal">String</code> and <code class="literal">Double</code>. We started with a list of <code class="literal">Strings</code>, so <code class="literal">A</code> was <code class="literal">String</code>. Then we prepended a <code class="literal">Double</code>, so the compiler inferred <code class="literal">B</code> to be <code class="literal">Any</code>, the closest (and only) common supertype.</p> <blockquote><p>will fix. </p> &#8212; deanwampler (2009-07-01 12:47:18) </blockquote> </div>urn:uuid:4f27c5ae-9316-363b-b773-6c7d7347ffc3deanwampler2009-07-01T12:47:18-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_the_operator_can_prepend_">The <code class="literal">::</code> method can prepend an object of a different type from <code class="literal">A</code>, the type of the elements in the original list. The compiler will infer the closest common supertype for <code class="literal">A</code> and the parameter <code class="literal">x</code>. It will use that supertype as <code class="literal">B</code>. Here&#8217;s an example that prepends a different type of object on a list.</p> <blockquote><p>"Closest" is the work I'm going with... </p> &#8212; deanwampler (2009-07-01 12:46:23) </blockquote> </div>urn:uuid:8171791d-7aa8-3df1-b1aa-3de08e68a9fedeanwampler2009-07-01T12:46:23-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_the_operator_can_prepend_">The <code class="literal">::</code> method can prepend an object of a different type from <code class="literal">A</code>, the type of the elements in the original list. The compiler will infer the closest common supertype for <code class="literal">A</code> and the parameter <code class="literal">x</code>. It will use that supertype as <code class="literal">B</code>. Here&#8217;s an example that prepends a different type of object on a list.</p> <blockquote><p>I'm planning to change "parser" to "compiler" globally. </p> <p>You're right that I picked the word "greatest" to have the opposite sense of the up/down sense of sub/super. Will fix. </p> &#8212; deanwampler (2009-07-01 12:45:42) </blockquote> </div>urn:uuid:e0107c5e-395d-30f9-8138-8b5f2ea72fd1deanwampler2009-07-01T12:45:42-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_these_bounds_are_called__upper">These bounds are called <span class="emphasis"><em>upper type bounds</em></span>, following the <span class="emphasis"><em>de facto</em></span> convention that diagrams of type hierarchies put subtypes below their supertypes. We followed this convention in the diagram shown 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>.</p> <blockquote><p>That's pretty much what I meant... </p> &#8212; deanwampler (2009-07-01 12:44:00) </blockquote> </div>urn:uuid:eb4ec2b7-8599-3c2a-93c9-816e82b8fec7deanwampler2009-07-01T12:44:00-07:00Comment on 'Type Bounds' <div>Chapter 12. The Scala Type System <p id="para_types_intro">Scala is a statically-typed language. Its type system is one of the most sophisticated in any programming language, in part because it combines comprehensive ideas from functional programming and object-oriented programming. The type system tries to be logically comprehensive, complete, and consistent. It exceeds limitations in Java&#8217;s type system while containing innovations which appear in Scala for the first time.</p> <blockquote><p>Suggest "... because it <em>comprehensively combines</em> ..." </p> &#8212; acruise (2009-07-01 09:49:28) </blockquote> </div>urn:uuid:49f67147-3d2d-3427-bf4b-ac41c124dc65acruise2009-07-01T09:49:28-07:00Comment on 'Chapter 12. The Scala Type System' <div>Chapter 12. The Scala Type System <p id="para_types_intro">Scala is a statically-typed language. Its type system is one of the most sophisticated in any programming language, in part because it combines comprehensive ideas from functional programming and object-oriented programming. The type system tries to be logically comprehensive, complete, and consistent. It exceeds limitations in Java&#8217;s type system while containing innovations which appear in Scala for the first time.</p> <blockquote><p>"Its" type system. :) </p> &#8212; acruise (2009-07-01 09:48:56) </blockquote> </div>urn:uuid:b7746452-e2be-3eb3-a82a-c7b4a1659c47acruise2009-07-01T09:48:56-07:00Comment on 'Chapter 12. The Scala Type System' <div>Type Bounds <p id="para_you_would_use_the_underscore_">The underscore &#8216;_&#8217; is used for <span class="emphasis"><em>existential types</em></span>, which we&#8217;ll cover below, in <a class="xref" href="ch12.html#ExistentialTypes" title="Existential Types">the section called &#8220;Existential Types&#8221;</a>.</p> <blockquote><p>I'm not sure if this is right. I think the _ is only used in the case of existential types (where it expands to a forSome clause). Are you sure it's also used in pattern matching? You may well be right, but it might be worth checking. </p> &#8212; hellige (2009-07-01 09:41:23) </blockquote> </div>urn:uuid:487c0d1f-a26a-3f11-af32-791e057529e6hellige2009-07-01T09:41:23-07:00Comment on 'Type Bounds' <div>Variance Under Inheritance <p id="para_the_last_line_does_not_compile">The last line, #5, does not compile because we are attempting to use a covariant argument in a contravariant position. We&#8217;re also attempting to use a contravariant return value where only covariant values are allowed.</p> <blockquote><p>Good suggestion. That is a clearer example. Will change. </p> &#8212; deanwampler (2009-07-01 09:12:25) </blockquote> </div>urn:uuid:0fb63b27-19fd-381e-8eb4-1c7a2e1a2d1cdeanwampler2009-07-01T09:12:25-07:00Comment on 'Variance Under Inheritance' <div>Type Bounds <p id="para_a__view__is_a_value_of_functio">A <span class="emphasis"><em>view</em></span> is an implicit value of function type that converts a type <code class="literal">A</code> to <code class="literal">B</code>. The function has the type <code class="literal">A =&gt; B</code> or <code class="literal">(=&gt; A) =&gt; B</code> (recall that <code class="literal">(=&gt; A)</code> is a <span class="emphasis"><em>by-name parameter</em></span>). An in-scope implicit method with the same signature can also be used as a view, <span class="emphasis"><em>e.g.,</em></span> an implicit method imported from an <code class="literal">object</code>. The term <span class="emphasis"><em>view</em></span> conveys the sense of having a view from one type (<code class="literal">A</code>) to another type (<code class="literal">B</code>).</p> <blockquote><p>Both function values and methods must be marked "implicit" in order to be considered as views, right? Or maybe this has changed? If so, I'd say "A view is a value of function type marked 'implicit' that..." and "An in-scope method with the same signature and the 'implicit' qualifier is also a view." </p> &#8212; hellige (2009-07-01 08:57:15) </blockquote> </div>urn:uuid:dfc4150d-2328-3b0c-91b4-b4ec48cc7e3bhellige2009-07-01T08:57:15-07:00Comment on 'Type Bounds' <div>Variance Under Inheritance <p id="para_class_list_is_declared_list">Class <code class="literal">List</code> is declared <code class="literal">List[+A]</code>, which means that <code class="literal">List[String]</code> is a subclass of <code class="literal">List[AnyRef]</code>, so <code class="literal">Lists</code> are covariant in the type parameter <code class="literal">A</code>. (When a type like <code class="literal">List</code> has only one covariant type parameter, you&#8217;ll often hear the shorthand expression &#8220;Lists are covariant&#8221; and similarly for types with a single contravariant type parameter.)</p> <blockquote><p>Good point, and your right that the next example makes the point. Will clarify. </p> &#8212; deanwampler (2009-07-01 08:51:32) </blockquote> </div>urn:uuid:8c7df1de-cac6-389f-abb9-380c483dc121deanwampler2009-07-01T08:51:32-07:00Comment on 'Variance Under Inheritance' <div>Type Bounds <p id="para_the_new_list_of_type_list_any">The new list of type <code class="literal">List[Any]</code>, since <code class="literal">Any</code> is the closest common supertype of <code class="literal">String</code> and <code class="literal">Double</code>. We started with a list of <code class="literal">Strings</code>, so <code class="literal">A</code> was <code class="literal">String</code>. Then we prepended a <code class="literal">Double</code>, so the compiler inferred <code class="literal">B</code> to be <code class="literal">Any</code>, the closest (and only) common supertype.</p> <blockquote><p>Here, too, least rather than greatest. </p> &#8212; hellige (2009-07-01 08:47:41) </blockquote> </div>urn:uuid:199515da-2366-3ad2-85f4-40e6cbf1f105hellige2009-07-01T08:47:41-07:00Comment on 'Type Bounds' <div>Type Bounds <p id="para_the_operator_can_prepend_">The <code class="literal">::</code> method can prepend an object of a different type from <code class="literal">A</code>, the type of the elements in the original list. The compiler will infer the closest common supertype for <code class="literal">A</code> and the parameter <code class="literal">x</code>. It will use that supertype as <code class="literal">B</code>. Here&#8217;s an example that prepends a different type of object on a list.</p> <blockquote><p>Don't say "parser": the parser is not the type-checker, and I think it's needlessly confusing. Just say "the compiler will infer..." </p> <p>Also, this should be the "<em>least</em> common supertype," not the greatest. The greatest common supertype is always Any. </p> &#8212; hellige (2009-07-01 08:47:11) </blockquote> </div>urn:uuid:888a7a94-5554-31f2-85d0-0fb692ce082dhellige2009-07-01T08:47:11-07:00Comment on 'Type Bounds' <div>Understanding Parameterized Types <p id="para_a_declaration_like_list_043">Sometimes, a <span class="emphasis"><em>parameterized type</em></span> like <code class="literal">List</code> is called a <span class="emphasis"><em>type constructor</em></span>, because it is used to create specific types. For example, <code class="literal">List</code> is the type constructor for <code class="literal">List[String]</code> and <code class="literal">List[Int]</code>, which are different types. (Although they are actually implemented with the same byte code due to <span class="emphasis"><em>type erasure</em></span>.) In fact, it&#8217;s more accurate to say that all traits and classes are type constructors. Those without type parameters are effectively zero-argument, parameterized types.</p> <blockquote><p>Will follow up with you. Not sure I agree, but I could be wrong. Has happened in the past ;) </p> &#8212; deanwampler (2009-07-01 08:36:22) </blockquote> </div>urn:uuid:b0e1aa85-df62-3d8e-8dc4-e071d0497211deanwampler2009-07-01T08:36:22-07:00Comment on 'Understanding Parameterized Types' <div>Reflecting on Types <p id="para__any_isinstanceof_t_will_ret"><code class="literal">x.isInstanceOf[T]</code> will return true if the instance <code class="literal">x</code> is of type <code class="literal">T</code>. However, this test is subject to type erasure. For example, <code class="literal">List(3.14159).isInstanceOf[List[String]]</code> will return true, because the type parameter of <code class="literal">List</code> is lost at the byte code level. However, you&#8217;ll get an &#8220;unchecked&#8221; warning from the compiler.</p> <blockquote><p>Good idea. will add... </p> &#8212; deanwampler (2009-07-01 08:34:44) </blockquote> </div>urn:uuid:ef9f9ccb-f9f3-3b82-a4be-5a3e882572b9deanwampler2009-07-01T08:34:44-07:00Comment on 'Reflecting on Types' <div>Reflecting on Types <p id="para_note_that_these_methods_are_on">Note that these methods are only available on subtypes of <code class="literal">AnyRef</code>.</p> <blockquote><p>That's probably true, although "C.class" won't compile in Scala. What I just noticed however, is that classOf[C] and c.getClass return slightly different things: scala&gt; classOf[C] res0: java.lang.Class[C] = class C </p> <p>scala&gt; c.getClass res1: java.lang.Class[_] = class C </p> <p>scala&gt; C.class <console>:1: error: identifier expected but 'class' found. C.class </p> <p>Will need to comment that... </p> &#8212; deanwampler (2009-07-01 08:30:23) </blockquote> </div>urn:uuid:ce14dbc4-96b7-33f2-b8c0-a4235e81230cdeanwampler2009-07-01T08:30:23-07:00Comment on 'Reflecting on Types' <div>Type Bounds <p id="para_these_bounds_are_called__upper">These bounds are called <span class="emphasis"><em>upper type bounds</em></span>, following the <span class="emphasis"><em>de facto</em></span> convention that diagrams of type hierarchies put subtypes below their supertypes. We followed this convention in the diagram shown 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>.</p> <blockquote><p>Not to mention the fact that "sub-" and "super-" imply a vertical ordering... ;) </p> &#8212; hellige (2009-07-01 08:29:56) </blockquote> </div>urn:uuid:e2fd0fa6-a73e-3437-a94b-e7a2adc4e1d8hellige2009-07-01T08:29:56-07:00Comment on 'Type Bounds' <div>Variance Under Inheritance <p id="para_the_last_line_does_not_compile">The last line, #5, does not compile because we are attempting to use a covariant argument in a contravariant position. We&#8217;re also attempting to use a contravariant return value where only covariant values are allowed.</p> <blockquote><p>I find the last few paragraphs somewhat confusing. The talk about "substituting the supertype" and so on sort of seems to imply substituting a <em>value</em> of that type, which is not what you mean. Also, the setup with the applyFunc function adds extra complication. I think it might be clearer if you just show a series of assignments: val f: C =&gt; C = (x:C) =&gt; c val f2: C =&gt; C = (x:CSuper) =&gt; cSub val f3: C =&gt; C = (x:CSub) =&gt; c // illegal This focuses on the essentials, which to me at least, makes it clearer. </p> <p>The following paragraphs are much clearer and give a good intuition. </p> &#8212; hellige (2009-07-01 08:25:20) </blockquote> </div>urn:uuid:81a2e4f4-6451-3fa6-8fb8-25861d90c30ehellige2009-07-01T08:25:20-07:00Comment on 'Variance Under Inheritance' <div>Variance Under Inheritance <p id="para_the_declarations_function__n_">The traits <code class="literal">FunctionN</code>, for <code class="literal">N</code> equals 0 to 22, are used by Scala to implement function values as true objects. Let&#8217;s pick <code class="literal">Function1</code> as a representative example. It is declared <code class="literal">trait Function1[-T, +R]</code>.</p> <blockquote><p>Example of my last comment. Given what you've said so far, I don't know what to say: is Function1 covariant or contravariant? I'm confused! Of course the answer is it's contravariant in T and covariant in R... </p> &#8212; hellige (2009-06-30 15:03:26) </blockquote> </div>urn:uuid:c06db92f-32ef-33a9-86a0-e5b11265e25ehellige2009-06-30T15:03:26-07:00Comment on 'Variance Under Inheritance' <div>Variance Under Inheritance <p id="para_class_list_is_declared_list">Class <code class="literal">List</code> is declared <code class="literal">List[+A]</code>, which means that <code class="literal">List[String]</code> is a subclass of <code class="literal">List[AnyRef]</code>, so <code class="literal">Lists</code> are covariant in the type parameter <code class="literal">A</code>. (When a type like <code class="literal">List</code> has only one covariant type parameter, you&#8217;ll often hear the shorthand expression &#8220;Lists are covariant&#8221; and similarly for types with a single contravariant type parameter.)</p> <blockquote><p>I would explicitly state that it doesn't <em>really</em> make sense to say that a type by itself is co/contra-variant: it only makes sense to say that it is covariant with respect to a particular parameter. Then I'd note that sometimes we're sloppy for types with one parameter (like List) and we just say "List is covariant" rather than "List is covariant in its only parameter". </p> <p>I realize that it sounds stuffy and pedantic, but I think it's better to be clear and explicit: this stuff is confusing enough already! </p> &#8212; hellige (2009-06-30 15:01:36) </blockquote> </div>urn:uuid:0ad02460-3841-3f70-b832-ebc50275981ehellige2009-06-30T15:01:36-07:00Comment on 'Variance Under Inheritance' <div>Understanding Parameterized Types <p id="para_a_declaration_like_list_043">Sometimes, a <span class="emphasis"><em>parameterized type</em></span> like <code class="literal">List</code> is called a <span class="emphasis"><em>type constructor</em></span>, because it is used to create specific types. For example, <code class="literal">List</code> is the type constructor for <code class="literal">List[String]</code> and <code class="literal">List[Int]</code>, which are different types. (Although they are actually implemented with the same byte code due to <span class="emphasis"><em>type erasure</em></span>.) In fact, it&#8217;s more accurate to say that all traits and classes are type constructors. Those without type parameters are effectively zero-argument, parameterized types.</p> <blockquote><p>This terminology seems off to me. I would instead say that "List" is a type constructor, because it is used to create specific types. I don't know of any specific jargon for a declaration like "List[+A]", it's just a regular type declaration that happens to declare a type with an argument. </p> <p>In fact, technically, Int is usually regarded as a type constructor as well, so I find this whole thing a little confused. In any case I don't think it matches standard usage. Feel free to email me if you like. </p> &#8212; hellige (2009-06-30 14:54:20) </blockquote> </div>urn:uuid:6486197d-70f1-3038-ad23-f868c7ec8e24hellige2009-06-30T14:54:20-07:00Comment on 'Understanding Parameterized Types' <div>Reflecting on Types <p id="para__any_isinstanceof_t_will_ret"><code class="literal">x.isInstanceOf[T]</code> will return true if the instance <code class="literal">x</code> is of type <code class="literal">T</code>. However, this test is subject to type erasure. For example, <code class="literal">List(3.14159).isInstanceOf[List[String]]</code> will return true, because the type parameter of <code class="literal">List</code> is lost at the byte code level. However, you&#8217;ll get an &#8220;unchecked&#8221; warning from the compiler.</p> <blockquote><p>Maybe worth mentioning that it will also issue a warning... </p> &#8212; hellige (2009-06-30 14:44:01) </blockquote> </div>urn:uuid:b4d9611e-e4bb-316f-8ead-6b615fc34319hellige2009-06-30T14:44:01-07:00Comment on 'Reflecting on Types' <div>Reflecting on Types <p id="para_note_that_these_methods_are_on">Note that these methods are only available on subtypes of <code class="literal">AnyRef</code>.</p> <blockquote><p>Actually isn't it rather that classOf[C] == C.class? </p> &#8212; hellige (2009-06-30 14:42:43) </blockquote> </div>urn:uuid:d662376a-884e-3333-85dc-97433333504dhellige2009-06-30T14:42:43-07:00Comment on 'Reflecting on Types'