Another way around - generic classes and traits
We just saw the effect of genericity and it solved more than one problem, we wrote less code and achieved more. The serveMeal
function was a generic one because it takes type parameters, in our case A
and B
. It performs the intended logic, great! Let's talk about parameterized types. You know the type List
, right? Let's take a look at its declaration in the Scala standard library:
sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] with Serializable
Okay, the declaration seems far too complex, doesn't it? No, wait, we know what sealed means, we know why we used an abstract class, then the name List
, and then a few more declarations for showing inheritance relationships. But there's this thing called [+A]
in our declaration. Our job is to find out what this is and why we used it.
From the previous few topics, we gained an...