While it looks pretty straightforward, there are some interesting things going on here. The first thing to consider is that the 'var' keyword has some very cool properties (imo). The 'var' keyword defines a variable that is not soft-typed, but is dynamically typed. In other words, variables are given a permanent type when they are assigned a value of a specific type.
Consider 'strHello' declared on line 5. When the interpreter reads 'var strHello' it creates a new variable with type
unknown . At this point, we could have assigned it an integer value, and it would be an integer. But immediately following, the variable is assigned the value "Hello!" -- a string. So, it assigns the type of strHello to a string. Assigning it an integer value after this would not work.
Now look at line 8. Notice the 'strHello.Length()'? This works because each type is actually a class containing useful functions associated with that type. And like JavaScript, all variables are instances of the object class. In fact, 'var' itself is of the 'unknown' class -- meaning it too has some useful functions. I'll get to that later.
But why bother, you may ask? Why not just specify the type and be done with it? Well, for one thing, I think its a bit easier for new users to pick up on -- they needn't worry about type assignments because its all done automatically. Furthermore, because 'var' objects can be assigned a type dynamically, it may be useful in a script to assign a value without knowing its type, and then ask it later.
An example might be line 6. The IO object on line 6 is an interface object (included on line 3, naturally) which contains a method called Input(). This method gets input from the input stream (specified inside the C++ application, here it would probably be console or keyboard input) and assigns the value, in this case, to nTest. nTest has not been assigned a type yet. The IO.Input() method converts the input to the most appropriate type -- in this case, 3 alone is an integer, so the return type is an integer.
The program now knows that nTest is an integer. But the scripter does not. Well, we can find out by simply using the isInteger() method:
if ( nTest.isInteger() ) // do something integer-esqueend if
This works because the unknown type, 'var', is the base class of all types, and each type inherits methods from 'var' to determine what type of variable it is.
[edited by - irbrian on April 13, 2004 12:10:39 PM]
---------------------------Brian Lacy"I create. Therefore I am."