The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

123avi (31) [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 (116) [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 (31) [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 (116) [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 (
(b) Theorems for Free (A paper by Philip Wadler -

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 .. ...