How to get the definition of the transfer of account assignment values?

object TestEnum extends Enumeration{ val One = Value("One") val Two,Three= Value } println(TestEnum.One.getClass) println(TestEnum.One.getClass.getDeclaringClass)//get Enumeration 

So my question is: how to get the [TestEnum] class from TestEnum.One?

Thanks.

+3
source share
3 answers

I do not think that you, unfortunately, can. TestEnum.One is really just an instance of the Enumeration#Value class. Actually, it is much worse than just this - your enum values ​​are erased in the same way:

 object Weekday extends Enumeration { val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value } def foo(w: Weekday.Value) def foo(e: TestEnum.Value) //won't compile as they erase to same type 

Because the instances of your enum are just instances of Enumeration#Value , their declaration class is simply scala.Enumeration .

This is frustrating, but it seems that these scala listings are worse than worthless; if you pass them through serialization (at least in 2.7.7), you will also not be able to do equality checks!

+7
source

I am going to post a link to the type of Enum that I wrote due to the limitations you were talking about. I do not use the built-in enumeration type, since I find it very limited. Although it does not have the function you would like (get Enumeration from Element), it would be pretty trivial to add it. Feel free to use it anyway if you find it useful.

(source and test / example): http://gist.github.com/282446

BTW If you like and need help adding a container to EnumElement, let me know.

+2
source

In my opinion, there is a simple solution to not deal with Scala Enumerations: use Java enum-s instead. Scala has supported cross-compilation for some time, and it's easy to just add a Java enumeration to the Scala source folder.

+1
source

Source: https://habr.com/ru/post/1201434/


All Articles