Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

kotlin

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

  • What does the error message mean?
  • How can I use id() call as given in the example?
like image 925
Senthil Kumaran Avatar asked Dec 18 '25 23:12

Senthil Kumaran


1 Answers

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()
    }
}
like image 157
Simon Forsberg Avatar answered Dec 21 '25 18:12

Simon Forsberg