Hey guys I'm trying to convert an odd lambda expre...
# getting-started
a
Hey guys I'm trying to convert an odd lambda expression from a gradle plugin to Kotlin:
Copy code
project.getExtensions().configure(PublishingExtension.class, extension -> {
            extension.getPublications().registerFactory(MavenPublication.class, new MavenPublicationFactory(
                    dependencyMetaDataProvider,
                    instantiatorFactory.decorateLenient(),
                    fileResolver,
                    project.getPluginManager(),
                    project.getExtensions()));
            realizePublishingTasksLater(project, extension);
        });
However I'm having issues with the syntax for this in Kotlin.
The auto conversion tries to convert to this:
Copy code
project.extensions.configure(
            PublishingExtension::class.java,
            Action<PublishingExtension> { extension: PublishingExtension ->
                extension.publications.registerFactory<MavenPublication>(
                    MavenPublication::class.java,
                    MavenPublicationFactory(
                        dependencyMetaDataProvider,
                        instantiatorFactory.decorateLenient(),
                        fileResolver,
                        project.pluginManager,
                        project.extensions
                    )
                )
                realizePublishingTasksLater(project, extension)
            })
Which is unfortunately invalid syntax.
r
what's invalid about it? Look fine at first glance
a
Copy code
Type mismatch: inferred type is PublishingExtension!.(PublishingExtension) -> Unit but TypeVariable(T)!.() -> Unit was expected
That's the compilation error, however I have no clue what it means.
r
ah, it wants a lambda of type
T.() -> Unit
instead of
T.(T) -> Unit
Basically it tells you, it wants the
extension
parameter as
this
instead
Copy code
project.extensions.configure(
            PublishingExtension::class.java,
            Action<PublishingExtension> { 
                publications.registerFactory<MavenPublication>(
                    MavenPublication::class.java,
                    MavenPublicationFactory(
                        dependencyMetaDataProvider,
                        instantiatorFactory.decorateLenient(),
                        fileResolver,
                        project.pluginManager,
                        project.extensions
                    )
                )
                realizePublishingTasksLater(project, this)
            })
something like this should be minimal change to get it working I think (bunch of stuff is still extraneous tho)
a
Thanks, I get it now.
r
Copy code
project.extensions.configure<PublishingExtension> {
            publications.registerFactory<MavenPublication>(
                MavenPublication::class.java,
                MavenPublicationFactory(
                    dependencyMetaDataProvider,
                    instantiatorFactory.decorateLenient(),
                    fileResolver,
                    project.pluginManager,
                    project.extensions
                )
            )
            realizePublishingTasksLater(project, this)
        }
this should work too as configure should have reified version afaik and
registerFactory
will likely have one too, look if it has it
👍 1