I have been looking at some of the Play Slick examples for building the data access layer and found the following line in the CatDAO example a bit intriguing:
def insert(cat: Cat): Future[Unit] = db.run(Cats += cat).map { _ => () }
and I wonder what's the purpose of doing .map { _ => () }
UPDATE: running the following in the Scala interpreter provides some clue but still it is not entirely clear why it is needed in the insert method above.
scala> val test = Seq(1, 2, 3)
test: Seq[Int] = List(1, 2, 3)
scala> test map { _ => () }
res0: Seq[Unit] = List((), (), ())
Without that mapping db.run
method would return a number of records that were inserted into the database (returning type would be Future[Int]
then). Yet the author of the code doesn't need that value, and he would like his API to return Future[Unit]
instead. That's why he's using that map
, which maps Int
to Unit
in this case (()
is a value representing Unit
type).