Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the value(s) of a parameter declared with vararg in an enum in Kotlin

I am new to Kotlin and I have an enum containing many values, those values refer to different states my application has.

Now I need to log something whenever the app enters a state but some state in the enum can log more than one thing (based on other parameters coming from outside the app) and some state does not need to log something.

Here is my enum:

enum class StateName(vararg log: String) {
    FIRST_CONNECTION(), // no parameter here
    AUTHORIZATION_CHECK("message 1", "message 2"),
    HANDSHAKE_SUCCESS("message")
    //...
}

If had declared the enum with a single obligatory parameter StateName(var log: String) I could have used HANDSHAKE_SUCCESS.log to retrieve its value but with vararg the IDE (android studio) does not find log at all.

So how can I retrieve my string with something like log[0]?

Note:

  • My enum is already defined (without parameters) and in use in my application and I can't use a different method of changing the actual state/adding a logging functionality
  • I need to use Kotlin even if I just started learning as the project was written with this language so I just want to know if (and how) I can do that.
like image 887
Cliff Burton Avatar asked May 30 '19 13:05

Cliff Burton


1 Answers

You need to change it to:

enum class StateName(vararg val log: String)

This way you can access the log

like image 94
r2rek Avatar answered Oct 27 '22 13:10

r2rek