In the book Functional Programming in Scala I see the following signature:
def productMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[(A,B)]
The description says that:
if types A and B are monoids, then the tuple type (A, B) is also a monoid
I have a difficulty in understanding the following part:
A: Monoid[A]
A is of type Monoid which takes itself as a type parameter? How to understand that?
Here: A: Monoid[A]
the is just a variable name, it can also be written as a: Monoid[A]
or param1: Monoid[A]
. It it a bit of convention to give such variable names, for example in most cases variable name for a functor is F - F: Function[A]
and M for a Monad - M: Monad[A]
:
abstract class SomeClass[TC[_], A](param: TC[A]) {
implicit val M: Monad[TC]
... // other code
}
It's more readable and clear if you see something like M.point[TC]
where M is an instance of a Monad
.
Added
And A: Monoid[A]
is not a type annotation at all. Type parameters are written in square brackets:
def productMonoid [A,B] (params...), so in this variant:
def productMonoid[A: Monoid,B](params...)
Yes Monoid[A]
is a type class for A
type, it's desugared into:
def productMonoid[A,B](params...)(implicit val $ev: Monoid[A])
This means that Monoid
is a type constructor which takes a type A
and constructs a type Monoid[A]
for $ev
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.