1

I have some code that used to work under Scala 2.12 but now fails under 2.13.

Essentially my calls look like so:

Controller -> Service -> Actor -> Repo

My controller looks like this:

complete {
  dep.chunkService.getChunksWithSubtitlesByDistribution(id, chunkState).map(
    _.right.map(
    chunkMap => StatusResult(
                  Meta(OK),
                  chunkMap.extractChunkDtoList,
                  chunkMap.extractSubtitleDtoList)): ToResponseMarshallable)
}

And the error I get is:

scala.collection.MapView$MapValues cannot be cast to scala.collection.immutable.Map

It happens on this line of the controller:

  chunkMap => StatusResult(

The service method that is called by the controller has the signature:

def getChunksWithSubtitlesByDistribution(distributionId: UUID, state: Option[ChunkState]): 
        Future[Either[DistributionNotFoundError, Map[Chunk, List[Subtitle]]]]

The service sets up an Actor that makes the call to the Repo like so:


sender ! chunkRepo.getChunksWithSubtitlesByDistribution(distributionId, state).right.map(_.mapValues(_.sortWith((a, b) => a.start < b.start) ) )

It is that value which will be returned by chunkService.getChunksWithSubtitlesByDistribution

chunkRepo.getChunksWithSubtitlesByDistribution returns this:

result match {
   case Nil => Left(DistributionNotFoundError(distributionId))
   case rs =>
     Right(
       rs.flatten
         .groupBy { case (c, s) => c }
           .mapValues(pairs => pairs.map { case (c, ss) => ss }.flatten)
           .toMap
     )
}

Not sure where in the chain the "MapView" comes into play, or how to get it to convert to a "normal" map?

ThaDon
  • 7,826
  • 9
  • 52
  • 84
  • 1
    `_.mapValues` in `2.13` returns a **View** and is deprecated. Replace it with `.view.mapValues(f).toMap` – Luis Miguel Mejía Suárez Dec 14 '20 at 21:37
  • @LuisMiguelMejíaSuárez That did the trick! Please allow me the privilege of awarding you internet points. If you could, make your comment into an answer and I'll select it. – ThaDon Dec 14 '20 at 22:00
  • That on its own is not really worth it as an answer and I am pretty sure the question will be closed either as _"caused by a typo"_ or a duplicate that I am too lazy to search for :p - Anyways, I am glad it helped :) – Luis Miguel Mejía Suárez Dec 14 '20 at 22:05
  • @LuisMiguelMejíaSuárez it surprises me that they would change the return type and mark it as deprecated. Essentially this is breaking functionality between 2.12 and 2.13, so really they could have just removed the function all together as it doesn't even return the same type as it once did. – ThaDon Dec 14 '20 at 23:25
  • Well `2.13` break a lot of things just like any other major **Scala** release. – Luis Miguel Mejía Suárez Dec 15 '20 at 03:26
  • 1
    Does this help you? [MapView() in Scala](https://stackoverflow.com/q/65289749/2359227) – Tomer Shetah Dec 15 '20 at 06:10

1 Answers1

1

As a commenter has pointed out, the return type of mapValues(f) has changed between Scala 2.12 and 2.13. The code to correct is:

sender ! chunkRepo.getChunksWithSubtitlesByDistribution(distributionId, state)
  .right.map(_**.view**.mapValues(_.sortWith((a, b) => a.start < b.start) )**.toMap** )

**changed code**

ThaDon
  • 7,826
  • 9
  • 52
  • 84