简体   繁体   中英

How does this groovy syntax translate to kotlin?

I created a simple Greeting task in Kotlin.

Just like the one in the gradle documentation .

Now I'm adding a simple test to it that looks like this in groovy:

class GreetingTaskTest {
    @Test
    public void canAddTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        def task = project.task('greeting', type: GreetingTask)
        assertTrue(task instanceof GreetingTask)
    }
}

I translated this one into Kotlin except one single bit in this line:

def task = project.task('greeting', type: GreetingTask)

The problematic bit is the second parameter. type: GreetingTask

What does it exactly stand for and how does it translate into Kotlin?

Thx to Opal for leading me to the solution.

In addition to his answer here is the kotlin version of the test:

class GreetingTaskTest {
    @Test
    public fun canAddTaskToProject() {
        val project = ProjectBuilder.builder().build()
        val task = project.task(mapOf("type" to GreetingTask::class.java), "greeting")
        assertTrue(task is GreetingTask)
    }
}

Have a look at the docs . task method takes a String and a Map . greeting is an instance of String and type: GreetingTask is a named parameter which is converted to an instance of Map . So you should pass a Map as the second arg. Unfortunately, don't know how to declare a Map in kotlin. This method might be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM