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)
android kotlin
add a comment |
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)
android kotlin
add a comment |
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)
android kotlin
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
android kotlin
edited Nov 10 at 0:37
asked Nov 9 at 21:52
li2
9501021
9501021
add a comment |
add a comment |
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))
...
Yeah, I am working withsealed 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 useobject
-- 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
add a comment |
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))
...
Yeah, I am working withsealed 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 useobject
-- 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
add a comment |
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))
...
Yeah, I am working withsealed 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 useobject
-- 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
add a comment |
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))
...
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))
...
answered Nov 10 at 11:47
Alexey Romanov
104k25207347
104k25207347
Yeah, I am working withsealed 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 useobject
-- 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
add a comment |
Yeah, I am working withsealed 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 useobject
-- 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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