Definition of defaultsTo operator


#1

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
end
else return the string itself

is this correct?


#2

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.


#3

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