-
Notifications
You must be signed in to change notification settings - Fork 434
KT-20635 - Kotlin plugin extension with a method accepting Action<in TYPE> method does not get inferred as SAM #522
Comments
Thanks for the report @mkobit! |
`Action<T>` combined with `SamWithReceiverAnnotations` should cause the Kotlin compiler to always treat `Action<T>` as an alias to `T.() -> Unit`, in other words: ```kotlin typealias Action<T> = T.() -> Unit ``` Unfortunately that's currently not always the case and it depends on whether the callee is defined in Kotlin or not. When the callee is defined in Kotlin, trying to pass a lambda expression where a, say, `Action<CopySpec>` is expected will result in the following error: ``` Error: Type mismatch: inferred type is () -> ??? but Action<CopySpec> was expected ``` See gradle/kotlin-dsl-samples#522
I've updated my SamWithReceiver playground project to demonstrate the issue in the Kotlin compiler.
typealias Action<T> = T.() -> Unit Unfortunately that's currently not always the case and it depends on whether the callee is defined in Kotlin or not. When the callee is defined in Kotlin, trying to pass a lambda expression where a, say,
|
…citly needed cast for profile configuration The `Action {}` cast is unforunately needed right now for plugins written in Kotlin. issue gradle/kotlin-dsl-samples#522 Kotlin issue https://youtrack.jetbrains.com/issue/KT-20635
…citly needed cast for profile configuration The `Action {}` cast is unforunately needed right now for plugins written in Kotlin. issue gradle/kotlin-dsl-samples#522 Kotlin issue https://youtrack.jetbrains.com/issue/KT-20635
@bamboo can it be worked around using the experimental feature introduced in the 1.2.50? To enable this you'll need to add |
@ligee Won't this mean that users of the Gradle Kotlin DSL will all suddenly get warnings in IntelliJ IDE for using unstable language features? |
Expected Behavior
Action<in TYPE>
should behave same way in Groovy and Kotlin pluginsAction
methods().Type -> Unit
) or usingAction<>
types for best behavior across both Groovy and Kotlin build scriptsCurrent Behavior
Running
./gradlew -s --scan
with example project (steps shown at bottom to reproduce) results in exception for type mismatch.Build output
Context
Writing a plugin in Kotlin and wanted to provided a friendly DSL for both Groovy and Kotlin.
I believe that in order to get the friendly DSL in both Groovy and Kotlin an
Action<? super TYPE>
method andTYPE.() -> Unit
methods have to be added (not 100% on that).Steps to Reproduce (for bugs)
Action {
cast at https://github.com/mkobit/jenkins-pipeline-shared-library-example/blob/master/build.gradle.kts#L60Action
casting is removed, get above stack tracefun pluginDependencies(action: Action<in PluginDependencySpec>) { //... }
Your Environment
NOTE: Cannot build scan in this fashion with invalid a compiler failure in build script
The text was updated successfully, but these errors were encountered: