123avi (29) [Avatar] Offline
what is the difference if we define the calculateInterest like at listing 2.1
def calculateInterest[A <: InterestBearingAccount](account: A, period: DateRange) = ...

or like this
def calculateInterest (account: InterestBearingAccount, period: DateRange) = ...

is it just style ?
Debasish Ghosh (111) [Avatar] Offline
Thanks for the question. With the constraints specified as part of the generic type, you get more precise returned type information. Have a look at the following example ..

scala> :paste
// Entering paste mode (ctrl-D to finish)

sealed trait Shape
case class Square(s: Int) extends Shape
case class Rectangle(l: Int, b: Int) extends Shape

def makeList(s1: Shape, s2: Shape) = List(s1, s2)

def makeList1[S <: Shape](s1: S, s2: S) = List(s1, s2)

// Exiting paste mode, now interpreting.

defined trait Shape
defined class Square
defined class Rectangle
makeList: (s1: Shape, s2: Shape)List[Shape]
makeList1: [S <: Shape](s1: S, s2: S)List[S]

scala> makeList(Square(12), Rectangle(13, 12))
res5: List[Shape] = List(Square(12), Rectangle(13,12))

scala> makeList1(Square(12), Rectangle(13, 12))
res6: List[Product with Serializable with Shape] = List(Square(12), Rectangle(13,12))

scala> makeList1(Square(12), Square(12))
res7: List[Square] = List(Square(12), Square(12))

With the version makeList1, you get the exact type while with makeList you get the upper bound always.

123avi (29) [Avatar] Offline
Thank you very much , it does clarifies it. Just another question if I may - are there things that I can do the polymorphic version but not the other way ? is it always recommended to use the polymorphic version ?
Debasish Ghosh (111) [Avatar] Offline
In functional programming the most widely used polymorphism is the parametric polymorphism, where we parametrize the function with some type. This has a lot of advantages in making your code more typesafe. This property is popularly known as parametricity. I discuss this in some detail in the book. And there are some additional readings on this topic which you can go through for further details.

(a) a presentation by Tony Morris (http://yowconference.com.au/slides/yowlambdajam2014/Morris-ParametricityTypesAreDocumentation.pdf)
(b) Theorems for Free (A paper by Philip Wadler - http://ttic.uchicago.edu/~dreyer/course/papers/wadler.pdf)

In short it constrains your type to the one which you need for your model without giving you any additional power. Hence your code automatically benefits from some free theorems which Wadler discusses in the above paper.

Let me know if you need more clarifications on this subject. Very recently I have written a simple blog post explaining parametricity in simple terms. You can also have a look at this .. http://debasishg.blogspot.in/2015/06/baking-can-teach-you-bit-of.html ...