1

I have this method

private fun checkRoomIdAndFindCinemaAndCheckIfRoomExists(paramRoomId: String?, paramCinemaId: String?, roomExists: Boolean?) : Array<Any> {

    val roomId = ValidationHandler.validateId(paramRoomId, "room id")

    val cinema = cinemaService.getCinemaById(paramCinemaId)

    val roomExists = roomRepository.existsByIdAndCinemaId(roomId, paramCinemaId!!.toLong())

    return arrayOf(roomId, cinema, roomExists)
}

What i want to do here is add roomId as Long , cinema as object and roomExists as boolean into an array, and return type should be the array. How can i do that?

Later i want to access these from another method.

5
  • 2
    The accepted answer here is ugly, not type safe (not for creation, not for method signature, not obvious what index is what, and only is type safe when casting which is asking for a runtime error), it is not maintainable, not idiomatic. Please change the accepted answer to the suggestion to use a Triple or data class. Commented Dec 1, 2018 at 22:21
  • 3
    You asked an "X/Y question" where you ask a question that leads towards an answer, but you asked the wrong question. You should ask "Is there a way to avoid using an Array<Any> to hold these three typed items so I can pass them around as a group?" Because, please, for all that is good in the world, do not use Array<Any> ... Commented Dec 1, 2018 at 22:25
  • @JaysonMinard you are absolutely right, i am sorry for my bad question, and i read the the answer and i understand why use the method in the answer, thank you for reply Commented Dec 2, 2018 at 0:12
  • 1
    it is not a "bad question" it is just a "X/Y question" which is something we want to reach back behind to find out where you were coming from so we can help get the best answer for you! Good luck with Kotlin! Commented Dec 2, 2018 at 0:23
  • Thats true, and you also made me think more after what i am doing when i am creating methods , return types and things to keep track of in runtime. I appreciate for your answer. Commented Dec 2, 2018 at 0:26

2 Answers 2

1

I suggest to use idiomatic Kotlin code instead of what was suggested already. When you want to return multiple values from a function, you should utilize data functions or existing classes like Pair or Triple if sufficient. In this case, Triple helps you:

private fun checkRoomIdAndFindCinemaAndCheckIfRoomExists(
    paramRoomId: String?,
    paramCinemaId: String?,
    roomExists: Boolean?
): Triple<Long, Any, Boolean.Companion> {
    //TODO
    return Triple(roomId, cinema, roomExists)
}

The good thing is that you can be sure about the types and don't have to cast anything from an unsafe Array<Any>. Furthermore, data classes let you make use of destructuring as shown here:

val (roomId, cinema, roomExists) = 
    checkRoomIdAndFindCinemaAndCheckIfRoomExists("id1", "id2", true)
Sign up to request clarification or add additional context in comments.

12 Comments

It's not a created but a standard library class. If it weren't, I'd still suggest to do so
Take a look at one of OP's comment: this help me avoid rewriting same code in other methods. So what if he needs 4 or 5 values?
Then you create a data class for holding 4 to 5 values
Great and keep on going
This is exactly why data classes exist, is for cases like that. And if not, use Triple because at least it would be typed instead of an untyped array @forpas. Data classes are little effort but pay off heavily in value, and ARE the intended use case for situations like this. Why avoid the actual tool intended for the job with untyped arrays?
|
0

You can call the method like this:

val array = checkRoomIdAndFindCinemaAndCheckIfRoomExists(paramRoomId, paramCinemaId, roomExists)

The array's inferred type is Array<Any>.
You can access the items of the array:

val roomId = array[0] as Long 
val cinema = array[1] as YourObjectClass
val roomExists = array[2] as Boolean

3 Comments

Unsafe what? And since when Array<Any> and a as b is not idiomatic Kotlin?
can you be sure that the first element is a Long? Or that the second is what you want it to be?
Please do not create untyped arrays to hold parameter values, use either Pair, Triple or a custom data class. All of these were made specifically for cases such as this. By design, you should NOT use Array<Any>.

Your Answer

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