2

I have the following structure in a data class:

data class A(
    val b: Int,
    val c: C
) {
    data class B(
        val d: Int
    )

    data class C(
        val d: Int
    )  
}

and an instance of this class is being passed to a method which has the following signarure:

fun doSomethingMethod(object: A.B?): Mono<Unit> =
            // do something
            }

So now I am trying to initialize an instance of the data class A with only initializing B as wel but I dont understand how to do it. So far I have tried:

val testObject = A(A.B(5))

But its not working. Anyone has an idea?

4
  • When you say it's not working, what happens? Is there a compile error, or a runtime error? Commented Jun 17, 2020 at 13:03
  • I get a compile error @gidds Commented Jun 17, 2020 at 13:13
  • 1
    Please post the error. (Not only will it help us answer your questions, but future visitors may be searching on it.) Commented Jun 17, 2020 at 13:14
  • You shouldn't be using the object as it is a keyword in kotlin for marking a singleton or creating an anonymous object. Commented Jun 17, 2020 at 16:32

1 Answer 1

4

To create an object of nested data class just use next syntax:

val instance = OuterClass.NestedClass([params])

In your case it will be:

val b = A.B(5)

Complete example:

fun doSomethingMethod(b: A.B?): Mono<Unit> {
    // do something
}

val b = A.B(5)
val mono = doSomethingMethod(b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.