https://kotlinlang.org logo
Title
m

mbonnin

09/28/2017, 12:14 PM
does kotlin script support automatic SAM conversion ? I get an error when I do
val action = Action<String> { println(it) }
b

bamboo

09/28/2017, 12:16 PM
Action<T>
is treated specially in build scripts and in projects using the
kotlin-dsl
plugin
Whereas a regular SAM interface with a
fun execute(target: T): Unit
would be treated as
(T) -> Unit
,
Action<T>
is treated as
T.() -> Unit
m

mbonnin

09/28/2017, 12:18 PM
Ok thanks, time for me to dig in the Kotlin documentation !
Just curious: what is that for ?
b

bamboo

09/28/2017, 12:21 PM
This is so the
it.
qualifier is not needed when interacting with
Action<T>
based Gradle APIs, for instance, `Project.copy(Action<CopySpec>)`:
copy {
   from(“source”) // no `it.from`
   into(“target”)
}
m

mbonnin

09/28/2017, 12:22 PM
I see
b

bamboo

09/28/2017, 12:22 PM
Your snippet could be rewritten as:
val action: Action<String> = Action { println(this) }
action.execute("fooBar!")
m

mbonnin

09/28/2017, 12:23 PM
Works fine, thanks 🙂
I was tryring to call plugins.all {}. Removing
it
works fine
b

bamboo

09/28/2017, 12:24 PM
👍