Hi,
Am 20.04.20 um 12:28 schrieb Sven Schreiber:
Am 19.04.20 um 17:06 schrieb Allin Cottrell:
> On Wed, 8 Apr 2020, Sven Schreiber wrote:
>
>> I know that currently where inbundle(b, "m") returns 0 because m
isn't
>> there, exists(b.m) instead yields an error. But I guess that could be
>> changed if wanted.
>> And vice versa: inbundle(a, "x") yields an error if there is no bundle
>> a, whereas exists(a.x) just returns 0. But are those differences really
>> by design or just by historical coincidence?
>
> Good points. There's some redundancy here (particularly if we throw in
> typeof() as well).
Ah I hadn't even thought of typeof().
>
> * However, exists(b.m) should be non-committal, so should return 0 if
> either b doesn't exist or it doesn't have anything under the key
"m".
> That's now fixed in git.
OK, good.
Ah, this is good to know! Very convenient.
>
> As for exists() vs typeof() -- they're basically the same thing under
> two names: the same back-end, and exists() returns the same codes as
> typeof(), though the help text is a little coy about that!
> Perhaps exists() should just be documented as an alias.
Yes, probably. I guess it wouldn't hurt to be explicit in the
documentation about the construction 'if exists()' without checking the
return code in detail.
>
> Now talking of typeof(): it returns numeric type codes, which
> typestr() can "translate". Not all that convenient. We recently
> introduced the symbolic constants TRUE and FALSE. Would it be overkill
> to introduce a few more: NULL, SCALAR, SERIES, MATRIX, STRING, BUNDLE,
> ARRAY? In that case you could do things like
>
> if typeof(b.m) == MATRIX
>
Spontaneously I'm not sure if that's necessary. OTOH if something like
that is done I think it should go all the way and also differentiate
different types of arrays. Maybe typeof() could just accept an optional
second string literal argument which then triggers such an equality
check:
if typeof(b.m, "matrix") # returns 1 if b.m is a matrix
or:
typeof(bb, "bundles") # checks if bb is an array of bundles
In Python
there exists the isinstance() function taking 2 arguments,
namely the object itself and the string of a data type being checked --
so similar what Sven has in mind:
https://docs.python.org/3/library/functions.html#isinstance
However, I don't think it's clean to "augment" the typeof() function in
Sven's proposed way. Returning the type of some 'object' and checking
whether some 'object' is of a specified type are two different issues.
And I think it's much cleaner to have two separate functions for two
different issues.
Artur