0

Scenario: I have two Enumerations type: Schema and Table.

I want to bind every Table to a Specific Schema. So, actually I have implemented this:

object Table extends Enumeration {

  type Tabella = Value

  val TABLE1 = Value("TABLE1")
  val TABLE2 = Value("TABLE2")

  def getTable(myTable: Tabella): String = {
    myTable match {
      case Table.TABLE1  =>
        (Schema.SCHEMA1 + "." + Table.TABLE1)
      case Table.TABLE2  =>
        (Schema.SCHEMA1 + "." + Table.TABLE2)
    }
  }

}

and of course

object Schema extends Enumeration {

  type Schemi = Value
  val SCHEMA1 = Value("SCHEMA1")

}

Now everytime I need a table I call Table.getTable(Table.TABLE1), but it's pretty awful code, I know. I'd like to improve this. Any Help?

I'd love to have some sort of override def toString in such way I just put my Table.TABLE1 in code and return SchemaName.TableName, is this possible?

Tizianoreica
  • 2,142
  • 3
  • 28
  • 43
  • If you use case objects instead of Enumeration, you can define your own toString method. I'm not sure why it doesn't work with Enumerations -- I've tried. See here for an example (my own) of the technique: http://scalaprof.blogspot.com/2016/01/the-curious-incident-of-scala-enum.html – Phasmid Mar 16 '17 at 13:50
  • @Phasmid You can't because you'd have to override `Value`'s `toString`, not the enumeration's. – Alexey Romanov Mar 16 '17 at 19:20
  • @AlexeyRomanov so any advices? – Tizianoreica Mar 16 '17 at 20:27
  • @Phasmid Just follow Phasmid's approach. You can't change `Value'`s `toString` like this, but call it anything else. – Alexey Romanov Mar 17 '17 at 06:57

0 Answers0