I don't know why I thought this was possible: ```d...
# announcements
d
I don't know why I thought this was possible:
Copy code
data class Something(val somethingElse: SomethingElse, val andAnotherOne: AndAnotherOne)

val something = Something(SomethingElse(), somethingElse.anotherOne)
is there any reason why not (I mean being able to access the first val when initializing the second)? Why shouldn't this be like a regular `val`s inside the class?
m
Copy code
val something = SomethingElse().let{ Something(it, it.anotherOne)}
this?
you kinda can with a secondary constructor btw
Copy code
data class Something(val somethingElse: String, val andAnotherOne: Int){
    constructor(somethingElse: String) : this(somethingElse, somethingElse.length)
}
val something = Something(String())
like this
d
Yeah, I guess.. that's probably what I'll do, thanks! But you're saying that the instance of
SomethingElse
isn't available yet... I was just wondering why?
m
You and me both buddy
d
I've used this for fixtures with mocks before, and it worked...
Copy code
data class Fixture(
   val depOne: DepOne = mockk(),
   val subject: SUTClass = SUTClass(depOne)
)
It seems like when you just pass it, it's ok, but when accessing a property in it not?
a
A constructor call is a function call, and in kotlin all function arguments are evaluated before calling the function. The property isn't set until the constructor runs, so there's nothing to access while you're still evaluating arguments.
d
But passing that property is allowed (without accessing what's in it)?
That makes sense though @Adam Powell, thanks! I guess it's just an
invoke
call...
And thanks @Michael de Kaste for the suggestions!
a
You're not passing the property in the later example, you're defining the function implementation, and that implementation happens to reference a parameter name that shadows/matches the property name. It's all encapsulated inside the function call, whereas your original example would have mixed caller/callee
👍🏼 1