Enumeration and mapping with Scala 2.10

2019-01-28 06:27发布

I'm trying to port my application to Scala 2.10.0-M2. I'm seeing some nice improvements with better warnings from compiler. But I also got bunch of errors, all related to me mapping from Enumeration.values.

I'll give you a simple example. I'd like to have an enumeration and then pre-create bunch of objects and build a map that uses enumeration values as keys and then some matching objects as values. For example:

object Phrase extends Enumeration {
  type Phrase = Value
  val PHRASE1 = Value("My phrase 1")
  val PHRASE2 = Value("My phrase 2")
}

class Entity(text:String)

object Test {
    val myMapWithPhrases = Phrase.values.map(p => (p -> new Entity(p.toString))).toMap  
}

Now this used to work just fine on Scala 2.8 and 2.9. But 2.10.0-M2 gives me following warning:

[ERROR] common/Test.scala:21: error: diverging implicit expansion for type scala.collection.generic.CanBuildFrom[common.Phrase.ValueSet,(common.Phrase.Value, common.Entity),That]
[INFO] starting with method newCanBuildFrom in object SortedSet
[INFO]   val myMapWithPhrases = Phrase.values.map(p => (p -> new Entity(p.toString))).toMap
                                                 ^

What's causing this and how do you fix it?

2条回答
放我归山
2楼-- · 2019-01-28 07:17

As you can see from your error, the ValueSet that holds the enums became a SortedSet at some point. It wants to produce a SortedSet on map, but can't sort on your Entity.

Something like this works with case class Entity:

implicit object orderingOfEntity extends Ordering[Entity] {
  def compare(e1: Entity, e2: Entity) = e1.text compare e2.text
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-28 07:18

It's basically a type mismatch error. You can work around it by first converting is to a list:

scala> Phrase.values.toList.map(p => (p, new Entity(p.toString))).toMap
res15: scala.collection.immutable.Map[Phrase.Value,Entity] = Map(My phrase 1 -> Entity@d0e999, My phrase 2 -> Entity@1987acd)

For more information, see the answers to What's a “diverging implicit expansion” scalac message mean? and What is a diverging implicit expansion error?

查看更多
登录 后发表回答