build.gradle file, task using << notation, i

2020-07-02 09:27发布

I'm new to groovy and gradle and was wondering whether someone knew why my scriplet wasn't working (edit actually it does work but the warning still appears). This section is taken out of the build.gradle script and intellij highlights this and complains that it: "Cannot infer argument types". any help would be nice :)

task hellofun << {
    [silly:'billy'].each { k, v ->
        println "$k=$v"
    }
}

EDIT: I have submitted a bug request informing Intellij of this problem

EDIT: apparently this is a known bug I'll update this once the bug is fixed

5条回答
▲ chillily
2楼-- · 2020-07-02 09:42

If you are willing to disable the Groovy > Assignment Issues > Incompatible type assignments inspection, the warning goes away.

查看更多
一纸荒年 Trace。
3楼-- · 2020-07-02 09:48

In a highly dynamically typed language such as groovy where names and symbols are resolved in arbitrary ways It would be quite difficult to determine the validity of any statement ahead of time without running the program. even running the program would theoretically not divulge all possible permutations of input that may change the runtime validity of a statement.

I applaud intellij's attempt at this hard problem and can live with a few incorrect syntax warnings here and there.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-07-02 09:51

Untill the IntelliJ bug is fixed, there is a workaround: use doFirst instead of <<.

task hellofun() {
  doFirst {
    [silly:'billy'].each { k, v ->
      println "$k=$v"
    }
  }
}

Thanks for submitting the bug :)

查看更多
聊天终结者
5楼-- · 2020-07-02 10:00

For the workaround, you should really call was was indicated by the << (doLast)

 task hellofun() {
   doLast {
     [silly:'billy'].each { k, v ->
       println "$k=$v"
     }
   }
 }
查看更多
欢心
6楼-- · 2020-07-02 10:04

In Intelij press Double shift (search all) enter "Assignment Issues" and change Assignment Issues: Incompatible type assignments inspection to "OFF"

查看更多
登录 后发表回答