openhab / openhab-vscode

VS Code extension for openHAB configuration files
https://marketplace.visualstudio.com/items?itemName=openhab.openhab
Eclipse Public License 2.0
159 stars 47 forks source link

Unexpected error: "Cannot refer to the non-final variable x inside a lambda expression" #209

Open DiogenesFL17 opened 3 years ago

DiogenesFL17 commented 3 years ago

Issue

I receive a syntax error in VS Code (red squiggles) for variables which evaluate correctly, i.e. openHAB does not log an error when processing the *.rules file, and the logic executes as expected.

Detail

I use lambdas inside Rules mainly to switch between Items in the same Group. For instance, I have Group:DateTime groups which I loop through to calculate times at which lights will turn on and off at dawn and dusk. One of my lambdas formats a string variable in preparation for further logic. This variable is used multiple times as it loops through each Group

var String T_DawnOn_st = ""
// Run through each Dawn/Dusk light group
var GroupItem G_DDLights = ScriptServiceUtil.getItemRegistry.getItem(C_DawnDuskGrpName_st) as GroupItem
G_DDLights.members.forEach[ G_x |   
    var GroupItem G_x_tmp = G_x as GroupItem
    G_x_tmp.members.forEach[ I_x |
        if (I_x.name.endsWith(C_SuffixDawnOnTimeItm_st)) {
            // Next line presents the error on "T_DawnOn_st"
            T_DawnOn_st = I_x.state.format("%1$tH:%1$tM")
        }
        ...
    ]
  ]

The logic executes correctly, so I assume this error should be removed, as openHAB has no problem with the syntax. The following line T_DawnOn_st = I_x.state.format("%1$tH:%1$tM") produces the syntax error (red squiggles): Cannot refer to the non-final variable T_DawnOn_st inside a lambda expression (org.eclipse.xtext.xbase.validation.IssueCodes.invalid_mutable_variable_access)

Your Environment

openHAB 2.5.6 Release Build on ubuntu VS Code 1.42.1 on Windows 10 Pro Chrome 76.0.3809.146 Node.js 12.4.0 VS Code openHAB extension 0.7.0

Here's a link to a fellow openHABian who also had the issue.