Home > Mobile >  Use a object before initialized as other object argument
Use a object before initialized as other object argument

Time:06-28

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!

CodePudding user response:

I don't think this is possible, and even if you manage to do that, I'd recommend against it

You can (and should) use nullable when you have a reason to use it and this seems a good enough reason

But if you really want to avoid null (and all you needing is the order between objects), you could use a Pair<Header, Trailer>, a List<>, or even a new class as a wrapper:

class HeaderTrailerWrapper(val header: Header, val trailer: Trailer)

class Header
class Trailer

CodePudding user response:

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)
  • Related