java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.Tuple2

2,859 views
Skip to first unread message

Viktor Hedefalk

unread,
Dec 10, 2012, 10:14:18 AM12/10/12
to scala-user
Hi,

I get this weird ClassCastException on what I think is rather basic compiling code in Scala 2.9.2. I couldn't find anything by a simple google.

The stack top looks like this:

15:38:57.498 [qtp221252808-228 - /?foodinput=100+gram+k%C3%B6tt] ERROR net.liftweb.http.LiftRules - Exception being returned to browser when processing /: Message: java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to scala.Tuple2
se.kostbevakningen.model.interpretations.InterpretationExtractors$$anonfun$filterOf$1$$anonfun$apply$1.apply(InterpretationExtractors.scala:13)
scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:252)
scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
scala.collection.immutable.List.foreach(List.scala:76)
scala.collection.TraversableLike$class.filter(TraversableLike.scala:251)
scala.collection.immutable.List.filter(List.scala:76)
se.kostbevakningen.model.interpretations.InterpretationExtractors$$anonfun$filterOf$1.apply(InterpretationExtractors.scala:13)
se.kostbevakningen.model.interpretations.InterpretationExtractors$$anonfun$filterOf$1.apply(InterpretationExtractors.scala:13)
se.kostbevakningen.model.interpretations.InterpretationExtractors$$anonfun$exactlyOne$1.apply(InterpretationExtractors.scala:16)
se.kostbevakningen.model.interpretations.InterpretationExtractors$$anonfun$exactlyOne$1.apply(InterpretationExtractors.scala:16)
se.kostbevakningen.model.interpretations.InterpretationExtractors$Case1a$$anonfun$$init$$1.apply(InterpretationExtractors.scala:36)
se.kostbevakningen.model.interpretations.InterpretationExtractors$Case1a$$anonfun$$init$$1.apply(InterpretationExtractors.scala:36)
se.kostbevakningen.model.interpretations.InterpretationExtractors$InterpretationExtractor.unapply(InterpretationExtractors.scala:28)
se.kostbevakningen.parsers.KostbevakningenParser$$anonfun$6.apply(KostbevakningenParser.scala:88)
se.kostbevakningen.parsers.KostbevakningenParser$$anonfun$6.apply(KostbevakningenParser.scala:88)



The code that's failing is the closure passed to filter:

def filterOf(predicate: TokenMatch => Boolean) = (i: Interpretation) => i.filter(x => predicate(x._2))

It's just using a few type aliases

type Token = String
type Interpretation = List[(Token, TokenMatch)]


and it's called from

def exactlyOne(predicate: TokenMatch => Boolean) = (i: Interpretation) => filterOf(predicate)(i).size == 1

TokenMatch is just an algebraic datatype to represent some parse result.

This code is called from within extractors looking like:

abstract class InterpretationExtractor(matchFunc: Interpretation => Boolean) {
    def unapply(i: Interpretation) = {
      if (matchFunc(i)) {
        Some(i)
      } else None
    }
  }
object Case1a extends InterpretationExtractor(i => exactlyOne(ingredient)(i) && noUnmatched(i) && ………)
object Case1b extends InterpretationExtractor(…)


that are called from a big pattern match.

Is this runtime error something known and in that case, is there any known workaround? The code compiles fine and I'm not doing anything spectacularly weird so I'm very surprised over a ClassCastException from the generated code…? If there is no simple answer, please let me know and I'll try to see if I can isolate a small test case.

Thanks,
Viktor Hedefalk

Reply all
Reply to author
Forward
0 new messages