variable existence checking

Tony Balinski ajbj at free.fr
Tue May 29 18:59:37 CEST 2007


Quoting Thorsten Haude <yoo at vranx.de>:

> Hi,
>
> * A.V.Kuznetsov wrote (2007-05-29 13:27):
> >Is it possible to extend macro language by checking of a (global?)
> >variable existence? I think, such a checking is very helpful, since
> >macro execution is broken when undefined variable is called.
>
> I (with TK's help) did something similar for my Patch Collection:
>
https://sourceforge.net/tracker/index.php?func=detail&aid=1058246&group_id=11005&atid=311005
>
> From the documentation:
> **typeof( value )**
>   Returns a string indicating the type of a macro value. The value can be
>   any NEdit macro language value except for the return value of functions
>   without a return value. Examples:
>   typeof(2)                        # -> "INTEGER"
>   typeof($empty_array)             # -> "ARRAY"
>   typeof(get_range(10, 20))        # -> "STRING"
>   typeof(myArray["doesnotexist"])  # -> "UNDEFINED"
>
>
> >I.e. new logical operator !! "existence" can be defined.
>
> I don't think that would be necessary.

I would actually prefer an existence operator, but NOT !!. This has a definite
meaning: "is true" (in macro !!x is equivalent to x!=0). I have been thinking
along the lines of an "exists" operator "??" instead:

    ??var: return true (1) if var has a value, false (0) otherwise
    ??array[]: return true if array is an array
    ??array[key]: return (key in array)
    ??array[key1][key2]: return ((key1 in array) && (key2 in array))
(with my array.key language extension, also: array??.key)

This allows for testing of undefined values, which cannot be passed to
functions.

It cannot be used usefully to distinguish between a function call that has no
return value with one which does, since you cannot capture the missing return
value of a function!

I think a typeof function is also useful, but Thorsten's last example involves
passing a non-value as an argument to a function, currently not allowed. (If
it were allowed, a number of things would need to change, eg being able to
save an undefined value in an array. I understand Torsten's patch does
something like this.)

Just some thoughts...

Tony


More information about the Discuss mailing list