0

I am new to Kotlin and I wanted to return multiple values from a function.

I checked this post:

How do we return multiple values from a function in Kotlin like we do in swift? I am using a recursion function where I don't have to use var or val when getting return values.

My Code:

   private fun folderSizeInBytes(directory: File): Pair<Long, Int>  {
    var length: Long = 0
    var size : Int = 0
    for (file in directory.listFiles().orEmpty()) {
        if (file.isFile) {
            length += file.length()
            size++
        }
        else
            (size,length) = folderSizeInBytes(file)
    }
    return Pair(length, size)
}

But at (size,length) = folderSizeInBytes(file) there is an error, saying there is unexpected tokens. Is it possible to do this without recreating size and length or a Pair object?

  • I think only [destructuring declarations](https://kotlinlang.org/docs/reference/multi-declarations.html) are currently supported – user2340612 Jun 27 '18 at 07:03
  • You are getting error as you haven't assigned any variable. so `(size,length) = folderSizeInBytes(file)` change it to `val (l,s) = folderSizeInBytes(file)` and than you'll be able to use the l and s variables and perform the operations in it. – Brijesh Joshi Jun 27 '18 at 07:38

0 Answers0