Enumeration and mapping with Scala 2.10

后端 未结 2 1590
太阳男子
太阳男子 2020-12-21 04:50

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

相关标签:
2条回答
  • 2020-12-21 05:14

    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?

    0 讨论(0)
  • 2020-12-21 05:15

    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
    }
    
    0 讨论(0)
提交回复
热议问题