Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

map - Enumeration and mapping with Scala 2.10

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...