vendredi 27 novembre 2015

Scala dynamically access a field in a class

Consider a class with lots of values

class Test {
    val a1 = "test1"
    val a2 = "test2"
    ..
    ..
    val a25 = "test25"
}

Can a function like this be written to access the nth variable.

def getVar(n: Int, test: Test) = {
    test.("test"+n) //something like this to access the nth variable
}

I know this can be done with a collection but my question is can this type of reflection be done.





Aucun commentaire:

Enregistrer un commentaire