Definition of defaultsTo operator


I’ve been assuming that this operator applies to strings, with pseudo-code:

if string is empty begin
log second argument if it exists
return first argument
else return the string itself

is this correct?


It’s not just for strings, but applied so any null value. In fact, I use it most often for arrays and maps since they often cause problems down the road when they are undefined. For example:

v1 = some_func(<args>).defaultsTo({}, "some_func() returned null!");
v2 = v1.put(["flip"], "flop");

The put() would error if some_func() doesn’t reliably return a map value. This serves as a prophylactic against that.


Right, that makes sense. We’ll need to test for any null value