Vijay Innamuri Vijay Innamuri -4 years ago 59
Scala Question

scala case class with CircularFifoQueue

Scala compiler throws compilation error for the following

case class UserSession(userEvents: CircularFifoQueue<UserEvent>)

Following error messages are from 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>)

Answer Source

use [] for generics in scala not diamonds (<>) which is java thing.

so should be case class UserSession(userEvents: CircularFifoQueue[UserEvent])

simple generics example,

scala> class Bag[T]
defined trait Bag

scala> case class Apple()
defined class Apple

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download