0

I think the answer is probably 'no' as from what I can tell maths libraries don't seem to handle null, but what I'd like to have is to be handle null in these scenarios:

x=1
(y not defined)
z = sum(x, y)

with eval() ideally giving:

z=1

However, this throws an exception, as y is not defined.

In effect I'm defaulting 'y' to zero in that example, and that won't work in all scenarios.

So, what I suppose I'm asking is, is there an in-built way to define a default for a variable if it's not assigned a value?

If not, I'm assuming that this would be possible by writing a custom function. If someone could confirm this, I'd really appreciate it.

Thanks :-).

John Stephenson
  • 499
  • 4
  • 13

1 Answers1

1

Ok - I was looking through the release notes for v4 and saw it talking about the constants 'null, undefined' (and others). So, in order to solve this I have to define a variable as undefined (probably should have known this!).

(Note: I suspect these constants are available in previous version too.)

Then combined with a 'default' custom function I can get what I wanted:

x=1
y = null 
z = sum(x, def(y, 0))
z=1

where 'def' is defined and imported as below (making use of 'lodash'):

var customFunctions = {
  def: function (value, defaultValue) {
    return !_.isNil(value) ? value : defaultValue;
  }
};
math.import(customFunctions);

BOSH!

John Stephenson
  • 499
  • 4
  • 13