I've read about reflection in both Kotlin and Java documentation & getattr in Python examples but they all seem to lack my use case:
I have boolean variables like repetitionOfElementsInNewExerciseAllowed
in a ViewModel
class (Android programming) that I need to update from a different class like HomeFragment
. My unsexy approach was the following function in ViewModel
:
fun updateBoolElement (boolElementToUpdate:String, valueToUpdateWith:Boolean){
when (boolElementToUpdate){
"repetitionOfElementsInNewExerciseAllowed" -> repetitionOfElementsInNewExerciseAllowed=valueToUpdateWith
}
}
Instead of this I'm striving for a recursive approach to extract the variable's name whose value I want to change out of the string boolElementToUpdate
like this (pseudo code):
fun updateBoolElement (boolElementToUpdate:String, valueToUpdateWith:Boolean){
getAttributeReferenceByName(boolElementToUpdate) = valueToUpdateWith
}
Is reflection or any other structure in Kotlin capable of doing such wizardry? Thanks for any hint!
Aucun commentaire:
Enregistrer un commentaire