使用CircularFifoQueue

时间:2017-06-14 03:11:44

标签: scala scala-generics

Scala编译器会抛出以下

的编译错误
case class UserSession(userEvents: CircularFifoQueue<UserEvent>)

以下错误消息来自Scala REPL:

$scala -cp Downloads/commons-collections4-4.1.jar 
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

scala> import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.collections4.queue.CircularFifoQueue

scala> case class UserSession(userEvents: CircularFifoQueue<UserEvent>)
<console>:1: error: identifier expected but ')' found.
case class UserSession(userEvents: CircularFifoQueue<UserEvent>)
                                                               ^

scala> case class UserSession(userEvents: CircularFifoQueue<int>)
<console>:1: error: identifier expected but ')' found.
case class UserSession(userEvents: CircularFifoQueue<int>)

1 个答案:

答案 0 :(得分:1)

使用[]来表示scala中的泛型而不是镶嵌的东西(<>)。

所以应该是case class UserSession(userEvents: CircularFifoQueue[UserEvent])

简单的泛型示例,

scala> class Bag[T]
defined trait Bag

scala> case class Apple()
defined class Apple

scala> new Bag[Apple]()
res4: Bag[Apple] = Bag@1ef93e01

资源

http://www.scala-lang.org/old/node/113