package Kotlin101.Functions.Cascades

fun main (Args : Array<String>){
    /* This call utilizes extension function and infix call */
    var superman = Superman() with {
        name = "Lux Luthor"
        punch()
        kick()
    }
}

public fun <T>T.with(operations : T.() -> Unit) : T {
    this.operations()
    return this
}

public class Superman() {
    var name : String = "Clark Kent"

    public fun punch() : Unit = println("$name punches")
    public fun fly() : Unit = println("$name flies")
    public fun kick() : Unit = println("$name kicks")
}
package Kotlin101.Functions.Cascades

fun main (Args : Array<String>){
    /* This call utilizes extension function and infix call. It is handy to deal with pesky Java object initializations */
    var superman = Superman() with {
        name = "Lux Luthor"
        punch()
        kick()
        sidekick = Spiderman() with {
            special()
        }
    }
}

public fun <T>T.with(operations : T.() -> Unit) : T {
    this.operations()
    return this
}

public class Superman() {
    var name : String = "Clark Kent"
    var sidekick : Sidekick = Robin()

    public fun punch() : Unit = println("$name punches")
    public fun fly() : Unit = println("$name flies")
    public fun kick() : Unit = println("$name kicks")
}

trait Sidekick {
    public abstract fun special()
}

public class Spiderman() : Sidekick {
    var name : String = "Peter Parker"
    override fun special() = println("$name webs")
}

public class Robin() : Sidekick {
    var name : String = "Robin"
    override fun special() = println("$name is useless")
}
Add sub object creation in with example
Add sub object creation in with example

dodyg/Kotlin101,mewebstudio/Kotlin101