Comments on Programming Scalaurn:uuid:d4270e81-74da-3876-81a7-833d21ad24382009-08-11T20:58:27-07:00
<div>Constructing Traits <p id="para_traits_don_t_support_auxiliary">Traits don’t support auxiliary constructors, nor do they accept an argument list for the primary constructor, the body of a trait. Traits can extend classes or other traits. However, they can’t pass arguments to the parent class constructor (even literal values), so traits can only extend classes that have a no-argument primary or auxiliary constructor.</p>
<blockquote><p>oops copied the wrong section into the buffer, meant to copy this
</p>
<pre>
<p>scala> class X (i:Int) { <br />
| require(i > 10)
| def this(a:Int, b:Int) = {
| this(a) <br />
| } <br />
| } <br />
defined class X
</p>
<p>scala> trait Y extends X{} <br />
defined trait Y
</p>
</pre>
— maddalab (2009-08-01 13:12:41)
</blockquote>
</div>urn:uuid:30078908-c2cc-3d20-aa73-461407f0eb1dmaddalab2009-08-01T13:12:41-07:00Comment on 'Constructing Traits'
<div>Constructing Traits <p id="para_traits_don_t_support_auxiliary">Traits don’t support auxiliary constructors, nor do they accept an argument list for the primary constructor, the body of a trait. Traits can extend classes or other traits. However, they can’t pass arguments to the parent class constructor (even literal values), so traits can only extend classes that have a no-argument primary or auxiliary constructor.</p>
<blockquote><blockquote><blockquote><p>so traits can only extend classes that have a no-argument primary or auxiliary constructor
</p>
</blockquote></blockquote><p><pre>
scala> class X (i:Int)
defined class X
</p>
<p>scala> trait Y extends X{}
defined trait Y
</pre>
</p>
— maddalab (2009-08-01 13:09:09)
</blockquote>
</div>urn:uuid:65d0638d-3fcf-365b-b814-202ab0ddf476maddalab2009-08-01T13:09:09-07:00Comment on 'Constructing Traits'
<div>Introducing Traits <p id="para_a_class_can_extend_a_trait_and">A class can extend a trait and a trait can extend a class. In fact, our <code class="literal">Widget</code> class above could have been declared to be a trait.</p>
<blockquote><blockquote><blockquote><p>A class can extend a trait and a trait can extend a class. In fact, our Widget class above could have been declared to be a trait.
</p>
</blockquote></blockquote><p>Which is correct, except the semantics vary a little, extending a trait with a class or trait (in additional to the traditional inheritance semantics) restricts the type into which the trait can be mixed in.
</p>
<p><pre>
abstract class Person
defined class Person
</p>
<p>trait Speaker extends Person {}
defined trait Speaker
</p>
<p>abstract class Animal
defined class Animal
</p>
<p>class PhilosophicalFrog extends Animal with Speaker{}
<console>:7: error: illegal inheritance; superclass Animal
is not a subclass of the superclass Person
of the mixin trait Speaker
class PhilosophicalFrog extends Animal with Speaker{}
^
</p>
<p>class PhilosophicalMan extends Person with Speaker{}
defined class PhilosophicalMan
</pre>
</p>
— maddalab (2009-07-30 04:41:01)
</blockquote>
</div>urn:uuid:ce1f685a-fcd4-3ba0-ba2d-713784f6e838maddalab2009-07-30T04:41:01-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_into_this_statement_">into this statement,</p>
<blockquote><p>ahh got it, I had declared observers as a val instead of a var, the operation+assignment sugar is only available on vars, which makes sense.
</p>
— maddalab (2009-07-30 04:28:17)
</blockquote>
</div>urn:uuid:de740259-118d-3cf0-ba60-e8906ddfe1b8maddalab2009-07-30T04:28:17-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_into_this_statement_">into this statement,</p>
<blockquote><p>dope sorry. special by me, do not know what I was doing there, typed the wrong thing in the console
</p>
— maddalab (2009-07-30 04:21:46)
</blockquote>
</div>urn:uuid:5c01a496-7acf-379d-9164-4e6d1e24cbf4maddalab2009-07-30T04:21:46-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_into_this_statement_">into this statement,</p>
<blockquote><p>typo
</p>
<p>observers ::= observer
</p>
<p>should be
</p>
<p>observer :: observers
</p>
<p>there is no ::= method on a List and is there were
</p>
<p>observer ::= observers
</p>
<p>would be
</p>
<p>observer.::=(observers)
</p>
<p>where as
</p>
<p>observer :: observers
</p>
<p>is
</p>
<p>observers.::(observer)
</p>
— maddalab (2009-07-30 04:19:03)
</blockquote>
</div>urn:uuid:b3746640-0e18-3166-9757-99aa82ef5db7maddalab2009-07-30T04:19:03-07:00Comment on 'Introducing Traits'
<div>Constructing Traits <p id="para_notice_the_order_of_invocation">Notice the order of invocation of the class and trait constructors. Since the declaration of <code class="literal">C12</code> is <code class="literal">extends Base12 with T1 with T2</code>, the order of construction for this simple class hierarchy is left to right, starting with the base class <code class="literal">Base12</code>, followed by the traits <code class="literal">T1</code> and <code class="literal">T2</code>, and ending with the <code class="literal">C12</code> constructor body. (For constructing arbitrarily-complex hierarchies, see <a class="xref" href="ch07.html#Linearization" title="Linearization of an Object’s Hierarchy">the section called “Linearization of an Object’s Hierarchy”</a> in <a class="xref" href="ch07.html" title="Chapter 7. The Scala Object System">Chapter 7, <i>The Scala Object System</i></a>.)</p>
<blockquote><p>Hmm. Good question! I think there was a reason that vanished as I refactored this code! Will fix.
</p>
— deanwampler (2009-07-04 10:04:32)
</blockquote>
</div>urn:uuid:7ec2a4c1-509f-3f43-8ebd-e7ea29e027bddeanwampler2009-07-04T10:04:32-07:00Comment on 'Constructing Traits'
<div>Stackable Traits <p id="para_in_fact_super_will_be_bound">In fact, <code class="literal">super</code> will be bound when this trait is mixed into an instance that defines a concrete <code class="literal">click</code> method, such as <code class="literal">Button</code>. Therefore, we need an <code class="literal">abstract</code> keyword on <code class="literal">ObservableClicks.click</code> to tell the compiler (and the reader) that <code class="literal">click</code> is not yet fully implemented, even though <code class="literal">ObservableClicks.click</code> has a body.</p>
<blockquote><p>You could use a self type here. That's a topic that takes some explanation and discussion of design tradeoffs, which we come back to several times later in the book.
</p>
— deanwampler (2009-07-04 10:02:39)
</blockquote>
</div>urn:uuid:de007a91-18bc-3f53-875e-6321b88d5fd4deanwampler2009-07-04T10:02:39-07:00Comment on 'Stackable Traits'
<div>Introducing Traits <p id="para_next_we_declare_a_list_of_obs">Next, we declare a list of observers. We make it a <code class="literal">var</code>, rather than a <code class="literal">val</code>, because <code class="literal">List</code> is immutable, so we must create a new list when an observer is added using the <code class="literal">addObserver</code> method.</p>
<blockquote><p>I made that choice to minimize distractions required to introduce new library types to the reader, but a note would be a good way to bring up the topic without too much disruption.
</p>
— deanwampler (2009-07-04 09:55:37)
</blockquote>
</div>urn:uuid:8399ac9e-29ba-3b1e-8c66-33e60e85ffe7deanwampler2009-07-04T09:55:37-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_except_for_the_trait_keyword">Except for the <code class="literal">trait</code> keyword, <code class="literal">Subject</code> looks like a normal class. <code class="literal">Subject</code> defines all the members it declares. Traits can declare <span class="emphasis"><em>abstract</em></span> members, <span class="emphasis"><em>concrete</em></span> members, or both, just as classes can (see <a class="xref" href="ch06.html#OverridingMembers" title="Overriding Members of Classes and Traits">the section called “Overriding Members of Classes and Traits”</a> in <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> for more details). Also like classes, traits can contain nested trait and class definitions and classes can contain nested trait definitions.</p>
<blockquote><p>Actually, it's in Application Design, specifically "Self-Type Annotations and Abstract Type Members" where we revisit this particular issue of "Any" vs. "Subject" as the argument's type.
</p>
— deanwampler (2009-07-04 09:53:23)
</blockquote>
</div>urn:uuid:2ea7b2bd-feeb-33f4-8341-8d12fb3c7c3fdeanwampler2009-07-04T09:53:23-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_except_for_the_trait_keyword">Except for the <code class="literal">trait</code> keyword, <code class="literal">Subject</code> looks like a normal class. <code class="literal">Subject</code> defines all the members it declares. Traits can declare <span class="emphasis"><em>abstract</em></span> members, <span class="emphasis"><em>concrete</em></span> members, or both, just as classes can (see <a class="xref" href="ch06.html#OverridingMembers" title="Overriding Members of Classes and Traits">the section called “Overriding Members of Classes and Traits”</a> in <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> for more details). Also like classes, traits can contain nested trait and class definitions and classes can contain nested trait definitions.</p>
<blockquote><p>Alexander, There's actually a good reason involving the way things type check. We'll get to it in the Type System chapter. I'll put some text here to provide a "hint" about why we used Any.
</p>
— deanwampler (2009-07-04 09:47:49)
</blockquote>
</div>urn:uuid:469460df-d1e5-3463-be7f-1a781a02553bdeanwampler2009-07-04T09:47:49-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_there_s_a_lot_going_on_here_n">There’s a lot going on here. The primary constructor takes a <code class="literal">label</code> argument and a list of <code class="literal">callbacks</code> that are invoked when the button’s <code class="literal">click</code> method is invoked. We’ll explore this class in greater detail in <a class="xref" href="ch05.html" title="Chapter 5. Basic Object-Oriented Programming in Scala">Chapter 5, <i>Basic Object-Oriented Programming in Scala</i></a>. For now we want to focus on one particular problem. Not only does <code class="literal">ButtonWithCallbacks</code> handle behaviors essential to buttons (like clicking), it also handles notification of click events by invoking the callback functions. This goes against the <span class="emphasis"><em>Single Responsibility Principle</em></span> <a class="xref" href="apa.html#Martin2003">[Martin2003]</a>, a means to the design goal of <span class="emphasis"><em>separation of concerns</em></span>. We would like to separate the button-specific logic from the callback logic, such that each logical component becomes simpler, more modular, and more reusable. The callback logic is a good example of a <span class="emphasis"><em>mixin</em></span>.</p>
<blockquote><p>Mario, thanks for the catch. Will fix.
David, unfortunately, we can't put all this on one line, because it will be too long for the book format. There's obviously plenty of room on the web page. I'll think about alternatives.
</p>
— deanwampler (2009-07-04 09:45:37)
</blockquote>
</div>urn:uuid:eb7c4fe9-5e53-33b1-80e2-bc61623476b5deanwampler2009-07-04T09:45:37-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_the_with_keyword_is_analogou">The <code class="literal">with</code> keyword is analogous to Java’s <code class="literal">implements</code> keyword for interfaces. You can specify as many traits as you want, each with its own <code class="literal">with</code> keyword.</p>
<blockquote><p>Forget the first comment. Stackable traits / Linearization will be mentioned in a later section!
</p>
— magle (2009-06-24 03:15:57)
</blockquote>
</div>urn:uuid:665d4187-2bf6-3cd7-968b-424f7561db02magle2009-06-24T03:15:57-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_there_s_a_lot_going_on_here_n">There’s a lot going on here. The primary constructor takes a <code class="literal">label</code> argument and a list of <code class="literal">callbacks</code> that are invoked when the button’s <code class="literal">click</code> method is invoked. We’ll explore this class in greater detail in <a class="xref" href="ch05.html" title="Chapter 5. Basic Object-Oriented Programming in Scala">Chapter 5, <i>Basic Object-Oriented Programming in Scala</i></a>. For now we want to focus on one particular problem. Not only does <code class="literal">ButtonWithCallbacks</code> handle behaviors essential to buttons (like clicking), it also handles notification of click events by invoking the callback functions. This goes against the <span class="emphasis"><em>Single Responsibility Principle</em></span> <a class="xref" href="apa.html#Martin2003">[Martin2003]</a>, a means to the design goal of <span class="emphasis"><em>separation of concerns</em></span>. We would like to separate the button-specific logic from the callback logic, such that each logical component becomes simpler, more modular, and more reusable. The callback logic is a good example of a <span class="emphasis"><em>mixin</em></span>.</p>
<blockquote><p>I am afraid I found the formatting of the ButtonWithCallbacks class confusing. Perhaps it is because I am a Scala novice, and I don't expect arguments to follow the class definition. The first time I read through the ButtonWithCallbacks code it didn't understand that label and clickedCallbacks were constructor arguments, I thought they were members.
</p>
<p>I think that something like the following alternate formatting might be clearer.
</p>
<p>class ButtonWithCallbacks(val label: String,
val clickedCallbacks: List[() => Unit])
extends Widget {
</p>
— dhlbrk (2009-06-23 18:03:42)
</blockquote>
</div>urn:uuid:4e68048d-205c-3af8-a848-95a9fc690dd4dhlbrk2009-06-23T18:03:42-07:00Comment on 'Introducing Traits'
<div>Stackable Traits <p id="para_in_fact_super_will_be_bound">In fact, <code class="literal">super</code> will be bound when this trait is mixed into an instance that defines a concrete <code class="literal">click</code> method, such as <code class="literal">Button</code>. Therefore, we need an <code class="literal">abstract</code> keyword on <code class="literal">ObservableClicks.click</code> to tell the compiler (and the reader) that <code class="literal">click</code> is not yet fully implemented, even though <code class="literal">ObservableClicks.click</code> has a body.</p>
<blockquote><p>I'm not sure, but would it be another option here to declare a self type for trait ObservableClicks ( 'self: Clickable =>' ) that forces the trait to be mixed in at least to an instance of type Clickable (so ObservableClicks could be sure, that it will be mixed in to a Clickable instance and therefore is able to call super.click() in a safe way)?
</p>
— magle (2009-06-23 04:51:12)
</blockquote>
</div>urn:uuid:a90d6b73-b0bd-3387-8b4d-172079c0e0f0magle2009-06-23T04:51:12-07:00Comment on 'Stackable Traits'
<div>Introducing Traits <p id="para_the_with_keyword_is_analogou">The <code class="literal">with</code> keyword is analogous to Java’s <code class="literal">implements</code> keyword for interfaces. You can specify as many traits as you want, each with its own <code class="literal">with</code> keyword.</p>
<blockquote><p>It may be mentioned in a later section that traits are stackable. So if you mix in multiple traits which all implement the same method, a call to super.method() will invoke the method (with the same signature) on the next trait down the 'stack' (the order in which the mixed in traits are 'pushed onto the stack' is determined by a so called 'Linearization')
</p>
— magle (2009-06-23 00:38:41)
</blockquote>
</div>urn:uuid:5be72848-040b-3e41-b0d6-58a1e09d8387magle2009-06-23T00:38:41-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_next_we_declare_a_list_of_obs">Next, we declare a list of observers. We make it a <code class="literal">var</code>, rather than a <code class="literal">val</code>, because <code class="literal">List</code> is immutable, so we must create a new list when an observer is added using the <code class="literal">addObserver</code> method.</p>
<blockquote><p>I would also vote for a mutable List / ListBuffer in this case, since you don't construct the Observer's List at one time and use it unchanged afterwards but have a collection of observers that could change at any time.
</p>
— magle (2009-06-22 08:44:10)
</blockquote>
</div>urn:uuid:4ca132b6-9471-381b-8b5f-e70a2c02df0emagle2009-06-22T08:44:10-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_there_s_a_lot_going_on_here_n">There’s a lot going on here. The primary constructor takes a <code class="literal">label</code> argument and a list of <code class="literal">callbacks</code> that are invoked when the button’s <code class="literal">click</code> method is invoked. We’ll explore this class in greater detail in <a class="xref" href="ch05.html" title="Chapter 5. Basic Object-Oriented Programming in Scala">Chapter 5, <i>Basic Object-Oriented Programming in Scala</i></a>. For now we want to focus on one particular problem. Not only does <code class="literal">ButtonWithCallbacks</code> handle behaviors essential to buttons (like clicking), it also handles notification of click events by invoking the callback functions. This goes against the <span class="emphasis"><em>Single Responsibility Principle</em></span> <a class="xref" href="apa.html#Martin2003">[Martin2003]</a>, a means to the design goal of <span class="emphasis"><em>separation of concerns</em></span>. We would like to separate the button-specific logic from the callback logic, such that each logical component becomes simpler, more modular, and more reusable. The callback logic is a good example of a <span class="emphasis"><em>mixin</em></span>.</p>
<blockquote><p>Simple typo: 'when the button’s click method in invoked' is likely meant to be 'when the button’s click method is invoked'
</p>
— magle (2009-06-22 08:24:08)
</blockquote>
</div>urn:uuid:211037de-5642-3ca6-81d7-23ccc0e24594magle2009-06-22T08:24:08-07:00Comment on 'Introducing Traits'
<div>Constructing Traits <p id="para_notice_the_order_of_invocation">Notice the order of invocation of the class and trait constructors. Since the declaration of <code class="literal">C12</code> is <code class="literal">extends Base12 with T1 with T2</code>, the order of construction for this simple class hierarchy is left to right, starting with the base class <code class="literal">Base12</code>, followed by the traits <code class="literal">T1</code> and <code class="literal">T2</code>, and ending with the <code class="literal">C12</code> constructor body. (For constructing arbitrarily-complex hierarchies, see <a class="xref" href="ch07.html#Linearization" title="Linearization of an Object’s Hierarchy">the section called “Linearization of an Object’s Hierarchy”</a> in <a class="xref" href="ch07.html" title="Chapter 7. The Scala Object System">Chapter 7, <i>The Scala Object System</i></a>.)</p>
<blockquote><p>What is the point of creating two C12 objects? Should we've been surprised that the creation of the two objects followed the same execution path? Or did you mean to create the second object from a class like "class C21 extends Base12 with T2 with T1". If so, it might be good to rename Base12 to Base (and to remove the "After creating.." print lines).
</p>
— battisti (2009-06-13 17:28:53)
</blockquote>
</div>urn:uuid:86a17c91-12b8-3248-bd3a-6af16b99f17dbattisti2009-06-13T17:28:53-07:00Comment on 'Constructing Traits'
<div>Introducing Traits <p id="para_next_we_declare_a_list_of_obs">Next, we declare a list of observers. We make it a <code class="literal">var</code>, rather than a <code class="literal">val</code>, because <code class="literal">List</code> is immutable, so we must create a new list when an observer is added using the <code class="literal">addObserver</code> method.</p>
<blockquote><p>A short explanation of why you choose that approach (immutable datatype assigned to a 'var') instead of using a mutable datatype with an 'val' e.g. val observers = Array<a href="">Observer</a> would be interesting as a side note.
</p>
— battisti (2009-06-13 16:14:23)
</blockquote>
</div>urn:uuid:84bc6685-4f71-3a5f-87b0-d6000c26b036battisti2009-06-13T16:14:23-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_except_for_the_trait_keyword">Except for the <code class="literal">trait</code> keyword, <code class="literal">Subject</code> looks like a normal class. <code class="literal">Subject</code> defines all the members it declares. Traits can declare <span class="emphasis"><em>abstract</em></span> members, <span class="emphasis"><em>concrete</em></span> members, or both, just as classes can (see <a class="xref" href="ch06.html#OverridingMembers" title="Overriding Members of Classes and Traits">the section called “Overriding Members of Classes and Traits”</a> in <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> for more details). Also like classes, traits can contain nested trait and class definitions and classes can contain nested trait definitions.</p>
<blockquote><p>Why is the type of subject in:" type Observer = { def receiveUpdate(subject: Any) } " Any and not Subject?
</p>
— battisti (2009-06-13 16:10:13)
</blockquote>
</div>urn:uuid:c2290054-8f35-3a8e-9763-63da3b1a2fbebattisti2009-06-13T16:10:13-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_except_for_the_trait_keyword">Except for the <code class="literal">trait</code> keyword, <code class="literal">Subject</code> looks like a normal class. <code class="literal">Subject</code> defines all the members it declares. Traits can declare <span class="emphasis"><em>abstract</em></span> members, <span class="emphasis"><em>concrete</em></span> members, or both, just as classes can (see <a class="xref" href="ch06.html#OverridingMembers" title="Overriding Members of Classes and Traits">the section called “Overriding Members of Classes and Traits”</a> in <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> for more details). Also like classes, traits can contain nested trait and class definitions and classes can contain nested trait definitions.</p>
<blockquote><p>Thanks. will fix.
</p>
— deanwampler (2009-06-08 18:45:54)
</blockquote>
</div>urn:uuid:75fe3e81-6ca9-32bd-8543-610aa6f7bf79deanwampler2009-06-08T18:45:54-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_look_at_gui_text_field_class">Have a look at the following code for a button in a graphical user interface, which uses callbacks for “clicks”.</p>
<blockquote><p>We're following (or will follow..) a convention that file names ending in "-script.scala" are scripts while others are compiled files. Will add a description in the preface that explains this.
</p>
— deanwampler (2009-06-08 18:11:34)
</blockquote>
</div>urn:uuid:2ae21646-4549-31dc-bcb2-e42dbc88e839deanwampler2009-06-08T18:11:34-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_except_for_the_trait_keyword">Except for the <code class="literal">trait</code> keyword, <code class="literal">Subject</code> looks like a normal class. <code class="literal">Subject</code> defines all the members it declares. Traits can declare <span class="emphasis"><em>abstract</em></span> members, <span class="emphasis"><em>concrete</em></span> members, or both, just as classes can (see <a class="xref" href="ch06.html#OverridingMembers" title="Overriding Members of Classes and Traits">the section called “Overriding Members of Classes and Traits”</a> in <a class="xref" href="ch06.html" title="Chapter 6. Advanced Object-Oriented Programming In Scala">Chapter 6, <i>Advanced Object-Oriented Programming In Scala</i></a> for more details). Also like classes, traits can contain nested trait and class definitions and classes can contain nested trait definitions.</p>
<blockquote><p><em>concrete need the markup fixed (</em>concrete_??)
</p>
— parthm (2009-06-02 09:27:21)
</blockquote>
</div>urn:uuid:8a2ab4be-e960-3096-83d0-f5033bb9362aparthm2009-06-02T09:27:21-07:00Comment on 'Introducing Traits'
<div>Introducing Traits <p id="para_look_at_gui_text_field_class">Have a look at the following code for a button in a graphical user interface, which uses callbacks for “clicks”.</p>
<blockquote><p>It can be mentioned that the example below is not runnable.
</p>
— parthm (2009-06-02 09:17:22)
</blockquote>
</div>urn:uuid:c7222b59-2f89-306d-844a-90b00e8d151aparthm2009-06-02T09:17:22-07:00Comment on 'Introducing Traits'