The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Currency example page 337

1 reply on 1 page. Most recent reply: Sep 4, 2009 1:32 AM by Andrew Phillips

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Howard Lovatt

Posts: 321
Nickname: hlovatt
Registered: Mar, 2003

Currency example page 337 Posted: Feb 20, 2008 12:54 PM
Reply to this message Reply
Advertisement
I found a few things strange in the currency examples on page 337 onwards. In particular I found 3 things odd.

1. It is odd to use the abstract type member rather than a generic type, e.g. isn't the following simpler:

abstract class AbstractCurrency[Currency <: AbstractCurrency[Currency]](val amount: Long) {
  val designation: String
  
  def make(amount: Long): Currency
  
  def +(that: Currency) = make(amount + that.amount)
  
  def *(x: Double) = make((amount * x).toLong)
  
  override def toString = amount + " " + designation
}


2. On the subject of generics the scope of an inner type does not extend to the generic type, e.g.:

class Example[T <: Inner] {
  class Inner {}
}


Is illegal because Inner is not in scope when T is declared, but the equivalent is allowed for abstract types, e.g. the currency example in the book.

3. It is strange to introduce abstract types in the book without discussing the differences between abstract and generic types and giving examples of when one technique would be preferred to the next.

What are others thoughts?


Andrew Phillips

Posts: 3
Nickname: demobox
Registered: Sep, 2009

Re: Currency example page 337 Posted: Sep 4, 2009 1:32 AM
Reply to this message Reply
> 3. It is strange to introduce abstract types in the book
> without discussing the differences between abstract and
> generic types and giving examples of when one technique
> would be preferred to the next.

Agree very much with this point - in fact, I was on the verge of posting exactly the same alternative example using a type parameter.
Whilst the discussion on path-dependent types earlier in the chapter hints at at least one difference between type variables and parameters (i.e. you can reference type variables by e.g. the expression "Europe.Currency" with value "Euro"), I think it would certainly be beneficial to reiterate that.

Flat View: This topic has 1 reply on 1 page
Topic: typo found in chapter 30 on actor Previous Topic   Next Topic Topic: Chapter 9 Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use