You can pass context by defining extenstions inside the class + by using "Builder API". But I recommend you to avoid extensions like String.smt().
object ConnectionPool{
fun useConnection(body: Connection.() -> Unit){
val connection = Connection()
connection.body()
}
}
class Connection {
val field = 42
fun String.execute() {
println(field)
}
fun execute2(query: String){
println(field)
}
}
fun sample() {
ConnectionPool.useConnection {
"CREATE TABLE".execute()
execute2("CREATE TABLE")
}
}
Also you can avoid mutablelist if you use generateSequence{}
and map{}
generateSequence { if (resultSet.next()) resultSet else null }
.map{ resultSetMapper(it) }.toList()
And I would reccomend to put close connection to useConnection()
.