Adding `squared` to Scala Numbers with Implicts and Type Classes
- We bring
infixNumericOpsinto scope. - We then invoke
i * i, which is equivalent toi.*(i). - The
*can’t be found onisinceiis of typeA. - However, now that we have
infixNumericOpsin scope, the compiler can use that conversion to create an instance ofNumeric[A]#Ops. Numeric[A]#Opsdoes have*defined for typeA, thus, a newOpsis constructued, holding our valueiwithin it.- The compiler then applies the
*operator to to the second value ofiand we’re done.
Cooler, but a hell of a lot more complicated to explain :)
Page 2 of 2 | Previous page