简体   繁体   English

如何在Kotlin的枚举中检索用vararg声明的参数的值

[英]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. 我是Kotlin的新手,我有一个包含许多值的枚举,这些值指的是我的应用程序具有的不同状态。

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. 如果使用单个必需参数StateName(var log: String)声明了枚举,我本可以使用HANDSHAKE_SUCCESS.log来检索其值,但是使用vararg IDE(android studio)根本找不到log

So how can I retrieve my string with something like log[0] ? 那么如何用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. 我需要使用Kotlin即使我刚开始学习,因为项目是用这种语言编写的,所以我只想知道是否(以及如何)我能做到这一点。

You need to change it to: 您需要将其更改为:

enum class StateName(vararg val log: String)

This way you can access the log 这样您就可以访问日志了

You need to create a variable inside enum which will hold all the arguments like below: 你需要在枚举中创建一个变量,它将包含所有的参数,如下所示:

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

        public var logParams:Array<out String>

        init{
             logParams=log
        }

    }

And to print the param corresponding to particular enum, you can iterate like below: 要打印与特定枚举相对应的参数,您可以按以下方式迭代:

 for (item in StateName.AUTHORIZATION_CHECK.logParams) {
           println("item = "+item)
 }

kotlin 参考 function 带可变参数并转换数组<out to list<out< div><div id="text_translate"><p> 我已经根据这个主题<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本问题提出了问题</a></p><p>所以,我想提前问一下。 有人用数组和列表回答了这个问题</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我发现,当我引用带有 vararg 参数的 function 时</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 当我执行类似::functionA 的操作时,A 的类型将是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,当我执行类似操作时</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情况 1</em></strong>如果我使用 compose function 并接受 List 类型,它将显示类型不匹配,因为引用::functionA 将返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="图片1"></p><p> <strong><em>情况 2</em></strong>如果我使用 compose function 并接受 Array 类型,它将显示错误</p><p>不能使用“T”作为具体类型参数。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="图片2"></p><p> 在上一篇文章中,有人回答我将数组转换为列表。 但是如何将引用 function 与 vararg 参数与原始Array&lt;out to List &lt;out ? 当我引用 function 之类的 function 时,类型必须是Array&lt;out但我想将其插入到 compose function 中。 我必须转换它。 任何人都可以帮忙吗? 我在那儿呆了很长时间。 希望有人能救我!!</p></div></out> - kotlin reference function with vararg parameter and convert Array<out to List<out

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将vararg参数传递给Kotlin中的另一个函数时编译时间错误 - Compile time error in passing vararg parameter to another function in Kotlin kotlin 参考 function 带可变参数并转换数组<out to list<out< div><div id="text_translate"><p> 我已经根据这个主题<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本问题提出了问题</a></p><p>所以,我想提前问一下。 有人用数组和列表回答了这个问题</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我发现,当我引用带有 vararg 参数的 function 时</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 当我执行类似::functionA 的操作时,A 的类型将是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,当我执行类似操作时</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情况 1</em></strong>如果我使用 compose function 并接受 List 类型,它将显示类型不匹配,因为引用::functionA 将返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="图片1"></p><p> <strong><em>情况 2</em></strong>如果我使用 compose function 并接受 Array 类型,它将显示错误</p><p>不能使用“T”作为具体类型参数。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="图片2"></p><p> 在上一篇文章中,有人回答我将数组转换为列表。 但是如何将引用 function 与 vararg 参数与原始Array&lt;out to List &lt;out ? 当我引用 function 之类的 function 时,类型必须是Array&lt;out但我想将其插入到 compose function 中。 我必须转换它。 任何人都可以帮忙吗? 我在那儿呆了很长时间。 希望有人能救我!!</p></div></out> - kotlin reference function with vararg parameter and convert Array<out to List<out kotlin 中的字符串格式和可变参数 - String format and vararg in kotlin kotlin 中的 vararg 没用吗? - Is vararg in kotlin useless? 引用vararg函数参数Kotlin - Referring to vararg function parameters Kotlin Android 数据绑定:如何获取 kotlin 枚举类的字段值? - Android Databinding: how to get field value of kotlin enum class? 如何通过 kotlin 中的序数值获得枚举成员? - How can I get a enum member by the ordinal value in kotlin? 有没有办法在vararg参数中保留类型? - Is there a way to preserve the types in a vararg parameter? Kotlin:内部类如何访问在外部类中声明为参数的变量? - Kotlin: How can an inner class get access to a variable declared as a parameter in an outer class? Kotlin-vararg-&gt;更改查询参数值 - Kotlin - vararg -> change query param values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM