user3591466 user3591466 - 3 months ago 12
Scala Question

Why is there only 1 type parameter in scala list flatmap parameter signature

Why does

flatMap
for
List
takes only
B
as the type parameter

def flatMap[B](f: (A) => U[B]): U[B]


instead of

def flatMap[A, B](f: (A) => U[B]): U[B]

Answer

Because A is deduced from the type parameter defined on List:

sealed abstract class List[+A]

And since flatMap is defined for each element of type A in the List, there's no need to explicitly declare it.

Comments