tkachuko tkachuko - 1 year ago 77
Scala Question

Case class without parameters alternative

During some simple scala coding exercise I ran into ideological problem of case classes without parameters and constructor parameters duplication.

It all started with the following two quite simple classes:

trait Namespace

case class Reply[T](namespace: Namespace, correlation: String, data: Try[T])

abstract class Request(val namespace: Namespace, val id: String = UUID.randomUUID().toString) {

def success[T](data: T) = Reply(namespace, id, Try(data))

def failure(msg: String) = Reply(namespace, id, Failure(new RuntimeException(msg)))

Now let's assume i have an entity
and I want to add
class as a command to query all records of type
. In my current set up it would be easier to actually write the following:

case class All extends Request(Posts)

However in this case I get compiler warning that case classes without parameters are deprecated. So one might suggest to rewrite it into the following:

case object All extends Request(Posts)

However in this case object All will be instantiated only once along with its
field which would like to avoid having unique id for each request.

Could you please suggest a better way of representing
command so that it would not be required to duplicate constructor arguments?

Thanks in advance?

Answer Source

The actual warning is that

case classes without a parameter list are not allowed; use either case objects or case classes with an explicit `()' as a parameter list.

So give this class an empty parameter list, just as suggested by the compiler:

case class All() extends Requests(Posts)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download