Hullaballoonatic
12/04/2019, 5:11 PM{ ... } { ... }
I'm musing on the reason why you can't place multiple lambda arguments outside the function parameter parentheses:
myPairs.associateBy { (k, _) -> k } { (_, v) -> v } // does not compile
myPairs.associateBy({ (k, _) -> k }, { (_, v) -> v }) // compiles
nbd. just thinking
of course maybe it just doesn't read well, since parameters are separated by a comma, and sequential lambda arguments outside the parentheses would not.Mike
12/04/2019, 5:17 PM,
and (
for combining things, and {}
outside ()
for last is as much about DSL writing as anything else.Edgars
12/04/2019, 5:19 PMfun a(block: () -> Boolean): (() -> Boolean) -> Boolean = { block() }
fun test() {
a { true } { true } // Only one lambda expression is allowed outside a parenthesized argument list
a { true }.invoke { true } // OK
}
Hullaballoonatic
12/04/2019, 5:20 PMfoo(a, b, c) { it.bar() }
implies { it.bar() }
happens after foo
returns.Hullaballoonatic
12/04/2019, 5:21 PMa { true } { true }
streetsofboston
12/04/2019, 5:36 PMfoo { … lambda … }
, foo may not return before ‘lambda’ is executed. If it is synchronous, ‘lambda’ is executed while foo is executing. If it is asynchronous, ‘lambda’ may get executed at any time or never.karelpeeters
12/04/2019, 5:44 PMclass Foo: (Int) -> Int by { x: Int -> 2 * x } {
fun foo() = println("foo")
}
karelpeeters
12/04/2019, 5:55 PMclass Foo : (Int) -> Int by object : (Int) -> Int by object : (Int) -> Int by {
2 * it
} {
fun obj1() = println("obj")
} {
fun obj2() = println("obj")
} {
fun foo() = println("foo")
}
Hullaballoonatic
12/04/2019, 6:07 PMHullaballoonatic
12/04/2019, 6:09 PMkarelpeeters
12/04/2019, 6:10 PM(T) -> R
is actually an interface and { it * 2}
is just a value that happens to implement that interface.karelpeeters
12/04/2019, 6:11 PMobject: (Int) -> Int by x
just returns a value that also happens to implement that interface.karelpeeters
12/04/2019, 6:11 PMby x
expects a value that implements the interface.Hullaballoonatic
12/04/2019, 6:11 PMkarelpeeters
12/04/2019, 6:12 PM(T) -> R
interface.Hullaballoonatic
12/04/2019, 6:15 PMkarelpeeters
12/04/2019, 6:16 PMval lambda = { x: Int -> 2 * x }
println(lambda is (Int) -> Int)