# Complex generics

**URL:** <https://discuss.kotlinlang.org/t/complex-generics/15267>\
**Category:** Uncategorized\
**Created:** [December 3, 2019, 1:17pm UTC](https://discuss.kotlinlang.org/t/complex-generics/15267 "2019-12-03T13:17:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lubospecho](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@lubospecho](https://discuss.kotlinlang.org/u/lubospecho)\
**Post date:** [December 3, 2019, 1:17pm UTC](https://discuss.kotlinlang.org/t/complex-generics/15267/1 "2019-12-03T13:17:10Z")

</div>

Hello, I am writing some method which I wan’t to be using with dsl like code, however I am stuck on following problem. I have reduced the code that is troubling me to following

package somePackage

```
/**
 *
 */
class SomeClass(
    val someString: String? = null,
    val someInteger: Int? = null,
    val someBoolean: Boolean? = null
)

fun main() {
    val map: Map< String, SomeClass.() -> Any?> = mapOf(
        "someString" to {someString},
        "someInteger" to {someInteger},
        "someBoolean" to {someBoolean}
    )
}

```

The function of type `SomeClass.() -> Any?` passed as value to map should serve as projection function over instance of `SomeClass`. The projection function works fine if it is passed as single argument, however I need to have to populate the map with entries like above.

Idea does not show any errors in the editor, however when I try to compile the code, the compilation fails with following.

```
Error:(13, 51) Kotlin: Type inference failed. Expected type mismatch: inferred type is Map<String, () -> ???> but Map<String, SomeClass.() -> Any?> was expected
Error:(14, 26) Kotlin: Unresolved reference: someString
Error:(15, 27) Kotlin: Unresolved reference: someInteger
Error:(16, 27) Kotlin: Unresolved reference: someBoolean

```

EDIT: even the code completion in the editor works inside the lambda with receiver, so in the editor, the type inference works, so I don’t know why the compiler cannot infer it.

I have come to a workaround with using method reference, but the code just look more tidy with the lambda receiver function.

Example of what I am trying to achieve and comparison with method reference

```
private val validator = requiredFields(
    "someString" to SomeClass::someString,
    "someInteger" to SomeClass::someInteger
).all()

private val validator1 = requiredFields<SomeClass>(
    "someString" to { someString },
    "someInteger" to { someInteger }
).all()

```

Here, the parameter is vararg of Pair, but the problem is the same.

Is there any way to have the _DSL_ cleaner than with method reference?  
Why the lambda receiver version does not compile? Is it maybe made like this on purpose?

---

<div class="post-metadata">

**Author:** ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)\
**Post date:** [December 3, 2019, 5:17pm UTC](https://discuss.kotlinlang.org/t/complex-generics/15267/2 "2019-12-03T17:17:23Z")

</div>

From the announcement of [1.3.40 Early Access Preview](https://discuss.kotlinlang.org/t/kotlin-1-3-40-early-access-preview/12775)

> Early Access Preview for Kotlin 1.3.40 is now open. We begin stabilization process of [new type inference](https://youtrack.jetbrains.com/issue/KT-31507). Now _new type inference_ , which has been experimental since 1.3.0, is [enabled by default](https://youtrack.jetbrains.com/issue/KT-30453) in the plugin, but not for compilation. Thus, if code without errors in IDE does not compile, please, report the issue to our [issue tracker](https://kotl.in/issue). The feature can be disabled by unchecking _Enable new type inference algorithm for IDE analysis (experimental)_ checkbox in _Settings_ → _Build, Execution, Deployment_ → _Compiler_ → _Kotlin Compiler_ .

---

<div class="post-metadata">

**Author:** ![lubospecho](https://avatars.discourse-cdn.com/v4/letter/l/ee59a6/32.png) [@lubospecho](https://discuss.kotlinlang.org/u/lubospecho)\
**Post date:** [December 4, 2019, 7:58am UTC](https://discuss.kotlinlang.org/t/complex-generics/15267/3 "2019-12-04T07:58:26Z")

</div>

thank, I didn’t know about this.
