Marc Knaup
03/11/2020, 2:02 PMFoo.invoke()
rather than treating it as a trailing lambda since none is expected.
fun main() {
makeFoo() { // Too many arguments for public fun makeFoo(): Foo defined in root package in file main.kt
42
}
}
fun makeFoo() = Foo()
class Foo {
operator fun invoke(arg: () -> Int) {
println(arg())
}
}
Fleshgrinder
03/11/2020, 2:06 PM(makeFoo()) {}
work? It’s typical for IIFE to require some additional parenthesis to work properly because otherwise it’s impossible to parse. 😛Fleshgrinder
03/11/2020, 2:06 PMif () {} if () {} else {}
problem.Marc Knaup
03/11/2020, 2:06 PMdiesieben07
03/11/2020, 2:06 PMmakeFoo()() { ... }
also worksMarc Knaup
03/11/2020, 2:07 PMMarc Knaup
03/11/2020, 2:07 PMMarc Knaup
03/11/2020, 2:07 PMdiesieben07
03/11/2020, 2:07 PMval makeFoo get() = Foo()
diesieben07
03/11/2020, 2:07 PMmakeFoo { }
worksFleshgrinder
03/11/2020, 2:08 PMinline
too. 😉Marc Knaup
03/11/2020, 2:08 PMMarc Knaup
03/11/2020, 2:08 PMFleshgrinder
03/11/2020, 2:08 PMMarc Knaup
03/11/2020, 2:09 PMFleshgrinder
03/11/2020, 2:10 PMinvoke
actually require access to the innter state? Otherwise you could drop makeFoo
and move the invoke
to the companion.Marc Knaup
03/11/2020, 2:11 PMFleshgrinder
03/11/2020, 2:11 PMexpect
to some actual
. A companion operator fun invoke
is almost always nicer in a JVM project. 😉Marc Knaup
03/11/2020, 2:12 PMFleshgrinder
03/11/2020, 2:13 PMFleshgrinder
03/11/2020, 2:13 PMMarc Knaup
03/11/2020, 2:14 PMSomeClass.invoke(…)
or even SomeClass.Companion.invoke(…)
Fleshgrinder
03/11/2020, 2:15 PMMarc Knaup
03/11/2020, 2:16 PM