Pretty substitution for apply


#1

Hello, I think it would be great idea to add into kotlin-stdlib something like this:

public inline operator fun <T> T.invoke(block: T.() -> Unit): T { block(); return this }

It is analogous to the extension function apply() but more pretty!

val dataSource = BasicDataSource() {
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://domain:3309/db"
    username = "username"
    password = "password"
    maxTotal = 40
    maxIdle = 40
    minIdle = 4
}

(This example is taken from here)
What do you think about it? Is there are any chance that this feature would be added in the future? :slight_smile:


#2

It doesn’t look me so great, “apply” is clearer solution and it is easily distinguishable to “run”.

Override “invoke” globally require may lead to misunderstanding, this code works as expected if and only if “BasicDataSource” doesn’t declare “invoke” function, so this example is incomplete.