Members.filter for DateTime items

Hey @rlkoshak,

Looking for your expertise on this one. Getting 4 warnings that are tied to DateTime fields on OH 3.x, wonder how to resolve them?

There is no context to infer the closure's argument types from. Consider typing the arguments or put the closures into a typed context.

I’m using this syntax for each of them, I understand that Generic is not correct for DateTime field. What is the correct syntax?

			if (gDimmerLastChange.members.filter[ GenericItem s | s.state == NULL || s.state === null || s.state.toString() != 'UNDEF' ])	{ 
			
				s.postUpdate(new DateTimeType())
			}

Best, Jay

That’s an unusual construction.
The Group filter will return a list object.
The if() will try to evaluate the list as true or false; if the list is empty (null) I expect that will be false, not sure if any non-zero list would be true?
In either case, the list is then thrown away before executing the next line.

This might help if you really want to test list/no-list

This never happens and can be removed,

General guidance

I would imagine you want to forEach through your filtered list

This syntax is still throwing the same warnings.

gDimmerLastChange.members.forEach[ GenericItem s | s.postUpdate(new DateTimeType()) ]

Best, Jay

@rossko57 is absolutely right, the filter returns a List so you need to do a forEach on that filtered list assuming you want to do something with each member of the filtered list.

What happens if you leave out the “GenericItem” part? It’s not usually needed and it’s often better to not type things unless you really really have to. This isn’t a case where you usually have to.

However, if the warning is still there without the “GenericItem”, then I’m skeptical that this is the actual source of the warning.

It’s complaining that it can’t infer the type of the closure’s arguments. In this case GenericItem s is the argument to the closure. And by providing GenericItem as the type, you are typing the argument. In this context, for the filter, GenericItem is probably the right type to use. You don’t need to be more specific than that. But, as I said it’s not really needed because the .members returns a List<Item> so it already knows the type.

One note on the filter though. s.state will *never be null so that test is redundant. And it’s off to compare the state to NULL but then the string "UNDEF". Why not compare to UNDEF the same as you compare to NULL?

Or you can use if(s.state instanceof UnDefType) which covers both NULL and UNDEF.

For me it looks like he wants anyMatch or allMatch instead of filter … basically check if any entry or all entries in the group have an initialized state.

This will “do stuff” to every Group member. (which might be a good idea to get the basics working)
How do know the error relates to this line of the mystery rule?

If/when you want to filter, then filter first, and forEach through the resulting list.
MyGroup.members.filter[ i | <condition> ].forEach [ x | <do stuff> ]

I want to Thank everyone for their guidance. I used everybody’s suggestions and fixed my warnings and improved other code also with the feedback.

Best, Jay

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.