1

I have the following code snippet, that I would like to improve:

final case class GenderOps[F[_]]()
                                (implicit xa: Transactor[F],
                                 ev: Bracket[F, Throwable]) extends GenderDb[F] {
  override def create: F[Int] =
    sql"""
    CREATE TABLE interests
    (
      id smallserial NOT NULL,
      interest character varying(40) NOT NULL,
      PRIMARY KEY (id)
    )
    """.update.run.transact(xa)

  override def seed[L[_] : Foldable](v: L[Gender]): F[Int] = ???

As you can see on the code, the case class has empty parameter. Is it possible to avoi case class?

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • 2
    You could always make your functions themselves accept those implicit parameters – user Jun 03 '20 at 18:01
  • Does this answer your question? [How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?](https://stackoverflow.com/questions/13812172/how-can-i-create-an-instance-of-a-case-class-with-constructor-arguments-with-no) – Shankar Shastri Jun 03 '20 at 18:03
  • 2
    A case class doing processing seems weird ... – cchantep Jun 03 '20 at 18:26

2 Answers2

7

A case class should only be used for data modeling, not processing.

This would be a better way of molding this:

sealed trait GenderDb[F[_]] {
  def create: F[Int]

  def seed[L[_] : Foldable](v: L[Gender]): F[Int]
}

object GenderDb {
  def apply[F[_]](implicit xa: Transactor[F], ev: Bracket[F, Throwable]): GenderDb[F] = new GenderDb[F] {
    override def create: F[Int] =
      sql"""
      CREATE TABLE interests
      (
        id smallserial NOT NULL,
        interest character varying(40) NOT NULL,
        PRIMARY KEY (id)
      )
      """.update.run.transact(xa)

    override def seed[L[_] : Foldable](v: L[Gender]): F[Int] = ???
  }
}
  • I've got compiler error: `illegal start of statement (no modifiers allowed here) [error] override def create: F[Int] = [error] ^ ` – softshipper Jun 03 '20 at 19:44
  • One more question, why do you have defined as `sealed`? – softshipper Jun 03 '20 at 19:54
  • 1
    @zero_coding just to ensure that all the implementations are in that same file, if you want or need to extend it in other files, you can remove the `sealed`, it is really not necessary, it was just mechanical by my side. – Luis Miguel Mejía Suárez Jun 03 '20 at 20:18
4

Case classes without parameter list have been deprecated in Scala 2.10 and made illegal in 2.11. One of the reasons are they may lead to confusion, for example

case class Foo
Foo // does this refer to companion object or to construction via Foo.apply()?

Case class without parameter list or empty parameter list should likely be made case object instead

case object Foo // instead of case class Foo()

however objects do not take type parameters

case object Foo[T] // syntax error

Given the Ops postfix in the name GenderOps it seems your intention is to enrich some class with methods which indicates that case classes or case objects might not be the correct tool. Instead consider trait or perhaps implicit class if you would like to enrich via extension methods.

Mario Galic
  • 47,285
  • 6
  • 56
  • 98