Question
· Dec 9, 2016

Variable types, string vs number

Is there any way that I can check the native type of an object script variable? (similar to the typeof check in javascript)

 

I see that the DynamicArray class uses the type of the variable for its output, I would like to do something similar when I receive a value as an argument to my method.

USER>s var1 = 123  //No quotes, number type
 
USER>s var2 = "123" //quotes, string type
 

//DynamicArray can tell the difference
USER>s arr = []
 
USER>d arr.%Push(var1)
 
USER>d arr.%Push(var2)
 
USER>w arr.%ToJSON()
[123,"123"] //first value is output without quotes, second value is quoted

Discussion (3)0
Log in or sign up to continue

One way to do this is comparing $listbuilds of the variable in question. Note also that you can coerce any value to a number with the "+" operator. So, for example:

USER>set var = 1
 
USER>w $lb(var)=$lb(+var)
1  <-- var is a number.
USER>set var = "1"
 
USER>w $lb(var)=$lb(+var)
0  <-- var is a not a number.

There's not really a simple built-in function to do exactly what you want though (at least that I know of).