Am 20.12.2017 um 08:05 schrieb Schaff, Frederik:
Allin:
> if you're sure
> that foo exists but want to check if it actually contains any
> members/elements:
>
> nelem(foo) != 0
>
> The latter check will work for objects of type matrix, list, array
Ah, I had exactly this '!=' formulation before. Now, you
point out that it does not > > work for matrices,
No, I think you missed Allin's proviso above "if you're sure it exists".
A 'matrices' object is just a special array, so is supported for nelem,
but only if it exists.
There is an issue with 'exists' and the scope (function vs.
script/global). It seems > to be the case that exists checks, at least for lists, on
the global scope. Run the > > following short test script, to see what I mean.
I see what you mean, and I understand your confusion. However, the
background is different and there is no scoping issue here. (To verify,
you could rename the list in the outer scope, and I think the results
should stay the same.)
The thing is that lists are special, and therefore you shouldn't use
exists() on them *when checking for a function argument*, only nelem().
This is an exception, and as you saw before nelem() on another function
argument type which is 'null' wouldn't work. There was a thread about
these things about a year ago.
So, the bottom line for checking 'null' default values for function
arguments:
- almost always use exists() first
- but for lists use (only) nelem() directly
- [for lists that are not function args you should be able to use
exists() as well]
Again, I completely understand the confusion, this is the result of some
path dependency in the past.
But I just checked the built-in doc for exists(), and it doesn't mention
the limitation with respect to list function arguments. I think it would
be good to mention the problem there -- in general I cannot really find
an explanation of the list situation in the docs.
And another related doc issue: In ch.13 of the guide I think the
following is incomplete nowadays:
"For optional arguments in pointer form, and additionally for string or
list arguments in standard form (see section 13.4), the special default
value null."
Whereas 'null' is allowed for almost all types now, I think.
cheers,
sven