Variable typing

Robert Sneidar bobs at twft.com
Sat Mar 25 19:41:55 EST 2006


For those interested, I have written a function which will return as  
a single character the type of variable you pass it, or else "U" for  
undefined if it doesn't exist. Why you ask? Because sometimes you  
want to know if a variable has yet to be defined, and if so what the  
value is. This is particularly important with modular applications.  
And it's nice to be able to do it by calling a function to check. The  
big deal for me is that the app I am porting uses this technique to  
determine if a module has been initialized yet. Sure I could figure  
another way to do it, but my approach is to convert the source with  
minimal trauma. I will optimize it later.

The problem is, Revolution script is soooo friendly with variable  
typing that just passing an undefined variable name as an arguement  
to a function will simply pass the variable name as a literal to the  
parameter of the called function, so within the function, no matter  
what, the value of the parameter will be something. It would be nice  
if Revolution did not do that, but I understand they need to be  
compatible with prior xCard solutions.

Once inside the function, variableNames and globalNames knows nothing  
of a local variable in the calling handler. And there is no way to  
distinguish if the parameter passed is the value of an existing  
variable, or the literal name of the variable I thought I was passing  
it. Revolution is simply being too friendly. Passing the variable as  
a reference doesn't help either. The parameter is still resolved to  
the value of the passed variable regardless, (which begs the question  
"what's the difference?") In short, there is no way for a called  
function to tell if a variable belonging to the scope of the calling  
script exists or not, and what it is. Until now.

The trick is to pass the variable AND the name of the variable as  
another variable:

on mouseUp
   put "novar" into somevar --a variable containing the name of the  
variable
   put theType(novar, somevar) into myType
end mouseUp

At this point, assuming the variable novar has not been defined  
anywhere, the following function will return "U".

function thetype @mVar, mvarName
   local mtype
   --Assume undefined until proven otherwise
   put "U" into mtype

   --see if it is a number
   if mvar is a number then
     put "N" into mtype
     return mtype
   end if

   -- Let's try logical
   if mvar is a boolean then
     put "L" into mtype
     return mtype
   end if

   -- How about a date?
   if mvar is a date then
     put "D" into mtype
     return mtype
   end if

   -- If mvar and mvarName are the same, the variable was undefined
   -- in the calling program, becase Revolution will assume you meant
   -- to pass a literal value as an arguement
   if mvar is not mvarname then
     put "C" into mvar
     return mvar
   end if

   return mtype
end thetype

So this is a 2 liner way to mimic the type() function in Foxpro. It's  
not perfect but it will save me a lot of coding.


Bob Sneidar
IT Manager
Logos Management
Calvary Chapel CM




More information about the Use-livecode mailing list