A better way to handle no Routines


(www.rboyapps.com - Make your home your butler!) #1

In some sample code from ST they get the list of Routines/Phrases in this manner:

def phrases = location.helloHome?.getPhrases()*.label

A system in which there are no routines, getPhrases() would return null. Now I know that the spread dot operator is null safe, but I don’t think it’ll work on a null object, it’ll just ignore element in an array.

Am I correct? If so maybe ST should change the sample code


(Eric) #2

Wouldn’t the ?.getPhrases() be ok due to the ?. safe navigation?


(www.rboyapps.com - Make your home your butler!) #3

I have a feeling getPhrases() is returning null and helloHome isn’t.

ie. it would be akin to (null)*.label where as *. is a safe navigator and will be able to handle [null]*.label but not the former if I’m correct.

You can run a quick test here:
https://groovyconsole.appspot.com/

This works:

def t = [[name:“test”, label:“joke”], null]
t*.label​

This doesn’t

def t = null
t*.label​