Kotlin-dsl-samples: DomainObjectCollection.all can't be called from kotlin with lambda

Created on 28 Aug 2018  路  2Comments  路  Source: gradle/kotlin-dsl-samples

Expected Behavior

val someDslSet = DefaultDomainObjectSet<Foo>()
someDslSet.all { //SAM conversion of Action<Foo>
  [...]
}

Current Behavior

When written as expected:

val someDslSet = DefaultDomainObjectSet<Foo>()
someDslSet.all { // Links to _Collections.kt Iterable<T>.all((T)->Boolean): Boolean
  [...]
}

Current workaround

val someDslSet = DefaultDomainObjectSet<Foo>()

@Suppress("ObjectLiteralToLambda")
someDslSet.all(object : Action<Foo> {
  override fun execute(target: Foo) {
    [...]
  }
}

Context

Trying to write a gradle plugin in kotlin that uses the kotlin-dsl plugin as part of the project configuration.

Steps to Reproduce (for bugs) && Environment

This commit in my project represents pretty well the good/bad/ugly of the current SAM conversion solution. https://github.com/trevjonez/android-components-plugin/commit/16ea739737cd1c9b8b817ab0c1cffbe601ed38fb

AndroidLibraryComponentsPlugin.kt had issues with DomainObjectCollection.all(Action<T>)
Whereas LibraryComponentFactory.kt was much improved.

question

Most helpful comment

Closing as answered

All 2 comments

DomainObjectCollection.all() takes an Action<T>. Action<T> is treated by Kotlin as equivalent to the functional type T.() -> Unit, that is, a Kotlin function type with a receiver. Therefore to use it you should use a lambda with receiver, that is without parameters:

domainObjectSet.all {
    // `this` is T
}

If you specify a lambda argument, then .all() resolves to _Collections.kt Iterable<T>.all((T)->Boolean): Boolean that takes an argument.

Closing as answered

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredsburrows picture jaredsburrows  路  3Comments

wuchengithub picture wuchengithub  路  4Comments

jaredsburrows picture jaredsburrows  路  3Comments

deeprim picture deeprim  路  4Comments

scompt picture scompt  路  4Comments