Senthil Kumaran
Senthil Kumaran

Reputation: 56941

Unit can't be called in this context by implicit receiver

I am following this Kotlin example (https://www.jetbrains.com/help/teamcity/kotlin-dsl.html#Editing+Kotlin+DSL) and trying to write a kotlin script for my CI.

This is my code snippet

steps {
    script {
        name = "Style check"
        id("StyleCheck")
        enabled = false
        scriptContent = """
            #!/bin/bash

            make docker run="make ci lint"
        """.trimIndent()
    }

I get an Error for the id() call which says

Error message

Upvotes: 17

Views: 18274

Answers (2)

Simon Forsberg
Simon Forsberg

Reputation: 13351

This error happens because the method id(String) is defined in an outer scope, and to prevent you from accidentally using the wrong method, Kotlin gives you a compiler error.

In your case you should make sure that there's no other id that you want to use. Perhaps you wanted to use the property named id instead of the method?


Note that neither of the options below might not have the same effect as you want. There might be a reason why the API is written like this to not allow you to call methods from outer receivers from inside inner scopes.

In order to use an explicit receiver, you can use this@methodName to call it. In this case, this@steps.

steps {
    script {
        name = "Style check"
        [email protected]("StyleCheck")
        enabled = false
        scriptContent = """
            #!/bin/bash

            make docker run="make ci lint"
        """.trimIndent()
    }
}

To understand exactly what's going on, and another way of using an explicit receiver, you could also do something like the below, where you save the this scope in a variable and then call it inside the script scope.

steps {
    val stepsThis = this
    script {
        name = "Style check"
        stepsThis.id("StyleCheck")
        enabled = false
        scriptContent = """
            #!/bin/bash

            make docker run="make ci lint"
        """.trimIndent()
    }
}

Upvotes: 7

Aadarsh Tiwari
Aadarsh Tiwari

Reputation: 33

I also got the same error when working with jetpack compose when I was writing the code below.

In my case the context was not clear.

It was giving this error.

'fun item(key: Any? = ..., content: LazyItemScope.() -> Unit): Unit' can't be called in this context by implicit receiver. Use the explicit one if necessary

It says Unit can't be called in this context. Therefore I changed the context and everything got right.

Error code:

LazyColumn {
       items(items = items) { word ->
           if (word != null) {
               WordColumnItem(word = word) {
                   onSelected(word)
               }
           }
           // Notice the context: within `items()` call
           if (items.itemCount == 0) {
               item(
                   content = { EmptyContent("No words") }
               )
           }
       }
   }

Correct code:

LazyColumn {
        items(items = items) { word ->
            if (word != null) {
                WordColumnItem(word = word) {
                    onSelected(word)
                }
            }
        }
        // Context changed: outside `items()` call
        if (items.itemCount == 0) {
            item(
                content = { EmptyContent("No words") }
            )
        }
    }

Although I don't know how to use the explicit receiver (if necessary).

Solution

  1. I think error is clear.
  2. You can use explicit receiver.

Upvotes: 0

Related Questions