Search code examples
scalaenumsscala-2.11

How to add a dot in scala enums values


I have a following enum

object TestKeys extends Enumeration{

    type  TestKeys = Value
    val _id , uuid Status.Date= Value
}

I need to add the dot between Status and Date but Eclipse is not allowing me .I have done some research and here. I found that there is a DescriptionAttribute in c# but its not working in Scala please help me how can i add dot in my enum values


Solution

  • You could use backticks around the name:

    scala> object TestKeys extends Enumeration{
               type  TestKeys = Value
               val _id, `Status.Date` = Value
           }
    defined object TestKeys
    

    Note however that there are some unexpected side effects:

    scala> TestKeys.withName("Status.Date")
    java.util.NoSuchElementException: No value found for 'Status.Date'
      at scala.Enumeration.withName(Enumeration.scala:124)
      ... 33 elided
    
    scala> TestKeys.withName("Status$u002EDate")
    res7: TestKeys.Value = Status$u002EDate
    
    scala> TestKeys.values
    res8: TestKeys.ValueSet = TestKeys.ValueSet(_id, Status$u002EDate)
    

    You can't have a . in a JavaIndentifier:

    scala> Character.isJavaIdentifierPart(46)  // 46 is '.'
    res16: Boolean = false