Guys, how do I initialize a bigger number of `val`...
# getting-started
h
Guys, how do I initialize a bigger number of
val
properties, if their values have to parsed during instance construction? Since I have to set the values in the constructor I ended up creating a helper class to return the values from my parsing function. There must be an easier solution, right? 😅 I was tempted to go with
var
properties, but in reality those are the classic `val`s
Copy code
class InitValues(var init1: String = "",
                 var init2: Boolean = true,
                 var init3: Int = 0,
                 var init4: String = "")

class Foo {
    val prop1: String
    val prop2: Boolean
    val prop3: Int
    val prop4: String

    constructor(initString: String) {
        val init = parseInitString(initString)
        prop1 = init.init1
        prop2 = init.init2
        prop3 = init.init3
        prop4 = init.init4
    }

    private fun parseInitString(initString: String): InitValues {
        val result = InitValues()
        // do some complicated parsing
        // ...
        result.init1 = "foo"
        result.init2 = false
        result.init3 = 42
        result.init4 = "bar"
        return result
    }
}