Kotlin: enums with associated values; function inside 'enum entry' gets `unresolved reference` error









up vote
0
down vote

favorite












I need to create a list of events with a string as name and a list of pairs as properties, some events properties are the static value, some need dynamically changed, so I create specific functions inside the enum entry to update it but complied with error unresolved reference:



Actually, what I want to implement is a list of enums with associated values,
something like these articles mentioned:




  • KT-4075 Allow setters overloading for properties, or


  • Kotlin: single property with multiple setters of different types, or

  • Using Kotlin’s sealed class to approximate Swift’s enum with associated data

Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation:



 enum class Event(val eventName: String, vararg eventProperties: Pair<String, String?>) 
LOGIN_CLICKED("Login", ("View" to "button clicked")),

LOGIN_SUCCEED("Login", ("Type" to "succeed")),

LOGIN_ERROR("Login")
fun errorMessage(errorMessage: String)
eventProperties = listOf("ErrorType" to errorMessage)

,

// ... some other events

LIST_ITEM_CLICKED("LIST")
fun listItemName(itemName: String)
eventProperties = listOf("View" to itemName)

;

var eventProperties: List<Pair<String, String?>>? = listOf(*eventProperties)


// Although this approach can fix my problem, but I don't prefer it,
// because these functions are only meaningful to specific enum item,
// I don't want them be opened to all enum items.
//
// fun errorMessage(errorMessage: String)
// eventProperties = listOf("ErrorType" to errorMessage)
//

// fun listItemName(itemName: String)
// eventProperties = listOf("View" to itemName)
//


fun main(args: Array<String>)

// unresolved reference
println(Event.LOGIN_ERROR.eventProperties)
Event.LOGIN_ERROR.errorMessage("error password")
println(Event.LOGIN_ERROR.eventProperties)










share|improve this question



























    up vote
    0
    down vote

    favorite












    I need to create a list of events with a string as name and a list of pairs as properties, some events properties are the static value, some need dynamically changed, so I create specific functions inside the enum entry to update it but complied with error unresolved reference:



    Actually, what I want to implement is a list of enums with associated values,
    something like these articles mentioned:




    • KT-4075 Allow setters overloading for properties, or


    • Kotlin: single property with multiple setters of different types, or

    • Using Kotlin’s sealed class to approximate Swift’s enum with associated data

    Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation:



     enum class Event(val eventName: String, vararg eventProperties: Pair<String, String?>) 
    LOGIN_CLICKED("Login", ("View" to "button clicked")),

    LOGIN_SUCCEED("Login", ("Type" to "succeed")),

    LOGIN_ERROR("Login")
    fun errorMessage(errorMessage: String)
    eventProperties = listOf("ErrorType" to errorMessage)

    ,

    // ... some other events

    LIST_ITEM_CLICKED("LIST")
    fun listItemName(itemName: String)
    eventProperties = listOf("View" to itemName)

    ;

    var eventProperties: List<Pair<String, String?>>? = listOf(*eventProperties)


    // Although this approach can fix my problem, but I don't prefer it,
    // because these functions are only meaningful to specific enum item,
    // I don't want them be opened to all enum items.
    //
    // fun errorMessage(errorMessage: String)
    // eventProperties = listOf("ErrorType" to errorMessage)
    //

    // fun listItemName(itemName: String)
    // eventProperties = listOf("View" to itemName)
    //


    fun main(args: Array<String>)

    // unresolved reference
    println(Event.LOGIN_ERROR.eventProperties)
    Event.LOGIN_ERROR.errorMessage("error password")
    println(Event.LOGIN_ERROR.eventProperties)










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I need to create a list of events with a string as name and a list of pairs as properties, some events properties are the static value, some need dynamically changed, so I create specific functions inside the enum entry to update it but complied with error unresolved reference:



      Actually, what I want to implement is a list of enums with associated values,
      something like these articles mentioned:




      • KT-4075 Allow setters overloading for properties, or


      • Kotlin: single property with multiple setters of different types, or

      • Using Kotlin’s sealed class to approximate Swift’s enum with associated data

      Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation:



       enum class Event(val eventName: String, vararg eventProperties: Pair<String, String?>) 
      LOGIN_CLICKED("Login", ("View" to "button clicked")),

      LOGIN_SUCCEED("Login", ("Type" to "succeed")),

      LOGIN_ERROR("Login")
      fun errorMessage(errorMessage: String)
      eventProperties = listOf("ErrorType" to errorMessage)

      ,

      // ... some other events

      LIST_ITEM_CLICKED("LIST")
      fun listItemName(itemName: String)
      eventProperties = listOf("View" to itemName)

      ;

      var eventProperties: List<Pair<String, String?>>? = listOf(*eventProperties)


      // Although this approach can fix my problem, but I don't prefer it,
      // because these functions are only meaningful to specific enum item,
      // I don't want them be opened to all enum items.
      //
      // fun errorMessage(errorMessage: String)
      // eventProperties = listOf("ErrorType" to errorMessage)
      //

      // fun listItemName(itemName: String)
      // eventProperties = listOf("View" to itemName)
      //


      fun main(args: Array<String>)

      // unresolved reference
      println(Event.LOGIN_ERROR.eventProperties)
      Event.LOGIN_ERROR.errorMessage("error password")
      println(Event.LOGIN_ERROR.eventProperties)










      share|improve this question















      I need to create a list of events with a string as name and a list of pairs as properties, some events properties are the static value, some need dynamically changed, so I create specific functions inside the enum entry to update it but complied with error unresolved reference:



      Actually, what I want to implement is a list of enums with associated values,
      something like these articles mentioned:




      • KT-4075 Allow setters overloading for properties, or


      • Kotlin: single property with multiple setters of different types, or

      • Using Kotlin’s sealed class to approximate Swift’s enum with associated data

      Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation:



       enum class Event(val eventName: String, vararg eventProperties: Pair<String, String?>) 
      LOGIN_CLICKED("Login", ("View" to "button clicked")),

      LOGIN_SUCCEED("Login", ("Type" to "succeed")),

      LOGIN_ERROR("Login")
      fun errorMessage(errorMessage: String)
      eventProperties = listOf("ErrorType" to errorMessage)

      ,

      // ... some other events

      LIST_ITEM_CLICKED("LIST")
      fun listItemName(itemName: String)
      eventProperties = listOf("View" to itemName)

      ;

      var eventProperties: List<Pair<String, String?>>? = listOf(*eventProperties)


      // Although this approach can fix my problem, but I don't prefer it,
      // because these functions are only meaningful to specific enum item,
      // I don't want them be opened to all enum items.
      //
      // fun errorMessage(errorMessage: String)
      // eventProperties = listOf("ErrorType" to errorMessage)
      //

      // fun listItemName(itemName: String)
      // eventProperties = listOf("View" to itemName)
      //


      fun main(args: Array<String>)

      // unresolved reference
      println(Event.LOGIN_ERROR.eventProperties)
      Event.LOGIN_ERROR.errorMessage("error password")
      println(Event.LOGIN_ERROR.eventProperties)







      android kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 0:37

























      asked Nov 9 at 21:52









      li2

      9501021




      9501021






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted











          Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation




          Why wouldn't it? If you are bothered with slightly longer declarations:



          object LoginClicked : Event("Login", mapOf("View" to "button clicked")) 
          \ vs
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked"))


          you can create a helper enum class for them:



          sealed class Event(val eventName: String, val eventProperties: Map<String, String?>) 
          enum class Basic(val eventName: String, val eventProperties: Map<String, String?>)
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked")),
          LOGIN_SUCCEED("Login", mapOf("Type" to "succeed")),
          ...


          class BasicEvent(b: Basic) : Event(b.eventName, b.eventProperties)

          class LoginError(errorMessage: String) : Event("Login", mapOf("ErrorType" to errorMessage))
          ...






          share|improve this answer




















          • Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
            – li2
            Nov 10 at 14:33






          • 1




            Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
            – Alexey Romanov
            Nov 10 at 14:45










          • thanks for pointing that out!
            – li2
            Nov 10 at 14:49










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233687%2fkotlin-enums-with-associated-values-function-inside-enum-entry-gets-unresol%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted











          Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation




          Why wouldn't it? If you are bothered with slightly longer declarations:



          object LoginClicked : Event("Login", mapOf("View" to "button clicked")) 
          \ vs
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked"))


          you can create a helper enum class for them:



          sealed class Event(val eventName: String, val eventProperties: Map<String, String?>) 
          enum class Basic(val eventName: String, val eventProperties: Map<String, String?>)
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked")),
          LOGIN_SUCCEED("Login", mapOf("Type" to "succeed")),
          ...


          class BasicEvent(b: Basic) : Event(b.eventName, b.eventProperties)

          class LoginError(errorMessage: String) : Event("Login", mapOf("ErrorType" to errorMessage))
          ...






          share|improve this answer




















          • Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
            – li2
            Nov 10 at 14:33






          • 1




            Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
            – Alexey Romanov
            Nov 10 at 14:45










          • thanks for pointing that out!
            – li2
            Nov 10 at 14:49














          up vote
          1
          down vote



          accepted











          Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation




          Why wouldn't it? If you are bothered with slightly longer declarations:



          object LoginClicked : Event("Login", mapOf("View" to "button clicked")) 
          \ vs
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked"))


          you can create a helper enum class for them:



          sealed class Event(val eventName: String, val eventProperties: Map<String, String?>) 
          enum class Basic(val eventName: String, val eventProperties: Map<String, String?>)
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked")),
          LOGIN_SUCCEED("Login", mapOf("Type" to "succeed")),
          ...


          class BasicEvent(b: Basic) : Event(b.eventName, b.eventProperties)

          class LoginError(errorMessage: String) : Event("Login", mapOf("ErrorType" to errorMessage))
          ...






          share|improve this answer




















          • Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
            – li2
            Nov 10 at 14:33






          • 1




            Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
            – Alexey Romanov
            Nov 10 at 14:45










          • thanks for pointing that out!
            – li2
            Nov 10 at 14:49












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted







          Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation




          Why wouldn't it? If you are bothered with slightly longer declarations:



          object LoginClicked : Event("Login", mapOf("View" to "button clicked")) 
          \ vs
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked"))


          you can create a helper enum class for them:



          sealed class Event(val eventName: String, val eventProperties: Map<String, String?>) 
          enum class Basic(val eventName: String, val eventProperties: Map<String, String?>)
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked")),
          LOGIN_SUCCEED("Login", mapOf("Type" to "succeed")),
          ...


          class BasicEvent(b: Basic) : Event(b.eventName, b.eventProperties)

          class LoginError(errorMessage: String) : Event("Login", mapOf("ErrorType" to errorMessage))
          ...






          share|improve this answer













          Because I have more than 100 events, 95% of them are static, only several of them need to be updated during runtime, so sealed class might not suit my situation




          Why wouldn't it? If you are bothered with slightly longer declarations:



          object LoginClicked : Event("Login", mapOf("View" to "button clicked")) 
          \ vs
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked"))


          you can create a helper enum class for them:



          sealed class Event(val eventName: String, val eventProperties: Map<String, String?>) 
          enum class Basic(val eventName: String, val eventProperties: Map<String, String?>)
          LOGIN_CLICKED("Login", mapOf("View" to "button clicked")),
          LOGIN_SUCCEED("Login", mapOf("Type" to "succeed")),
          ...


          class BasicEvent(b: Basic) : Event(b.eventName, b.eventProperties)

          class LoginError(errorMessage: String) : Event("Login", mapOf("ErrorType" to errorMessage))
          ...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 11:47









          Alexey Romanov

          104k25207347




          104k25207347











          • Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
            – li2
            Nov 10 at 14:33






          • 1




            Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
            – Alexey Romanov
            Nov 10 at 14:45










          • thanks for pointing that out!
            – li2
            Nov 10 at 14:49
















          • Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
            – li2
            Nov 10 at 14:33






          • 1




            Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
            – Alexey Romanov
            Nov 10 at 14:45










          • thanks for pointing that out!
            – li2
            Nov 10 at 14:49















          Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
          – li2
          Nov 10 at 14:33




          Yeah, I am working with sealed class now. The reason I didn't consider using sealed class at the beginning was that I thought creat a hundred class only for a list of constants might a huge cost, but then I found that I can use object -- I am not 100% sure -- which costs might same like enum entries. saled class solved my problems. many thanks!
          – li2
          Nov 10 at 14:33




          1




          1




          Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
          – Alexey Romanov
          Nov 10 at 14:45




          Ah, OK. object does create a class file, so this cost does exist, but it isn't that large, even on Android. Still the solution in my answer would avoid it.
          – Alexey Romanov
          Nov 10 at 14:45












          thanks for pointing that out!
          – li2
          Nov 10 at 14:49




          thanks for pointing that out!
          – li2
          Nov 10 at 14:49

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233687%2fkotlin-enums-with-associated-values-function-inside-enum-entry-gets-unresol%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo