windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Having trouble passing a value in a Kotlin function. The variable being convert:(String)->R

Asked By: Anonymous

I have some code here that reads a file and converts it to a list of a type depending what is being passed to the function. The code for this function looks like so.

fun <R> readFile(path:String,convert:(String)->R): List<R> {
    val reader = BufferedReader(FileReader(File(path)))
    var line = reader.readLine()
    val result = mutableListOf<R>()
    while(line!=null) {
        result.add(convert(line))
        line = reader.readLine()
    }
    reader.close()
    return result.toList()
}

The problem I have is that I am unsure sure what to pass into the variable convert. I am trying to return back a list of Person objects back. Below is the code I tried myself to hit the function readFile

val listOfPerson = readFile("path/person.txt","Person")

With this I’m getting an error with my Type. Intellij is flagging "Required (String) -> ???"


Solution

Answered By: Anonymous

The type (String) -> R is a function type. It represents a function that takes a String as argument and returns R (which is generic and can be different on every call to readFile()).

In order to call readFile(), you need to pass a String and a function:

readFile("path/person.txt") { line -> /* whatever you need */}

Note that in Kotlin, the { line -> ...} is a lambda expression, i.e. a function literal. This particular lambda expression represents a function that takes the argument line and does whatever is in the body.

Lambda expressions can be passed out of the parentheses of the function call, but it’s still an argument to the function. The above code is equivalent to:

readFile("path/person.txt", { line -> /* whatever you need */})

In your specific case, the convert argument is a conversion function, so you need to provide some code that converts the line: String argument into whatever R type you need in the output list.

If you just want a list of the lines as strings (without conversion) you can simply provide the identity function:

readFile("path/person.txt") { line -> line }

Or in short:

readFile("path/person.txt") { it }

it is an implicit argument (a shortcut) for lambda expressions that represent functions with only 1 argument, which is the case here.

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno