Issue
I'm new to kotlin and I know that nullable are something I should'nt use as much as I would want too. So I was wondering if something like that would be possible.
class Header(var next: Trailer)
class Trailer(var prev: Header)
fun main() {
lateinit var trailer: Trailer
val header = Header(trailer)
trailer = Trailer(header)
}
Thank you for the time used to answer my question!
Solution
First of all, I suggest to re-consider if there is a better way. Such cyclic relations are generally problematic.
Secondly, I think the only non-hacky way to create this kind of object relationship is inside the constructor, for example:
class Header {
var next: Trailer = Trailer(this)
}
fun main() {
val header = Header()
val trailer = Trailer(header)
header.next = trailer
}
Still, initialization for these objects is very tricky, so I suggest to hide their constructors and encapsulate initialization inside a function. This way we can also turn both prev
and next
properties into val
, for example:
class Header internal constructor() {
val next: Trailer = Trailer(this)
}
class Trailer internal constructor(val pref: Header)
fun createHeaderTrailerPair(): Header = Header()
fun main() {
val header = createHeaderTrailerPair()
val trailer = header.next
}
If you need to pass any parameters to Header
or Footer
, you have to do this through both createHeaderTrailerPair()
function and Header
constructor:
class Header internal constructor(
val name: String,
trailerName: String
) {
var next: Trailer = Trailer(this, trailerName)
}
class Trailer internal constructor(
val pref: Header,
val name: String
)
fun createHeaderTrailerPair(headerName: String, trailerName: String): Header = Header(headerName, trailerName)
Answered By - broot
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.