2

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

Community
  • 1
  • 1
swaheed
  • 3,671
  • 10
  • 42
  • 103

2 Answers2

3

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
Marth
  • 23,920
  • 3
  • 60
  • 72
  • what is this $u002E ? can you please explain – swaheed Oct 08 '15 at 14:28
  • @user3801239: `u002E` is the unicode value for a dot `.`. You can check this by running `'\u002E'.toChar` in a scala console. As for the `$`, it's used to interpolate a scala value inside a `String`. – Marth Oct 08 '15 at 14:48
  • You can fix `withName` using following construction: `val \`Status.Date\` = Value("Status.Date")` – Ilya Bystrov May 26 '19 at 17:02
1

If I understood you correctly, you need a way to write an identifier with dots in Scala. If that is the case, you can try something like: `Status.Date`.

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