Enumeration and mapping with Scala 2.10

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 14:30:50
Matthew Farwell

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?

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!