Kotlin-dsl-samples: Map Gradle API Groovy named arguments to Kotlin named parameters

Created on 1 Aug 2018  路  1Comment  路  Source: gradle/kotlin-dsl-samples

Since #914 we generate kotlin extensions for the Gradle API mapping Groovy named arguments to Kotlin vararg of Pair.

This is not ideal. It would be nice if the Gradle methods taking Groovy named arguments were annotated with metadata about the name and type of expected arguments, allowing us to generate more type-safe kotlin extensions.

For example:

public void apply(@NamedArguments(ObjectConfigurationAction.class) Map<String, ?> options);

Or, more generally:

interface SomeInterface {

  interface NamedArgumentsForFunction {
    void name(String stringArgument);
    void type(@Nullable Class<?> classArgument);
  // etc...
  }

  ...

  public void function(@NamedArguments(NamedArgumentsForFunction.class) Map<String, ?> options);
}

So we can enhance the code generator to emit overloads such as:

fun SomeInterface.function(name: String, type: Class<*>? = null, vararg options: Pair<String, Any?>) =
  function(mapOf("name" to name, "type" to type, *options))
feature gradle-api kotlin-dsl-api invalid

Most helpful comment

We should also consider not offering these overloads at all. They often just add unnecessary way of doing things differently. The Kotlin DSL is a great opportunity to undo some of those mistakes.

>All comments

We should also consider not offering these overloads at all. They often just add unnecessary way of doing things differently. The Kotlin DSL is a great opportunity to undo some of those mistakes.

Was this page helpful?
0 / 5 - 0 ratings