Version 2 (modified by Chuck, 15 years ago) |
---|
Although you can explicitly declare the type of a local variable, you don't have to:
# the long way: i as int = 0 # the short, easy way: i = 0
The type of the variable on the left is taken to be the type of the expression on the right.
This also works for object variables:
class Person var _name = '' # typed as String var _age = 0 # typed as int
If the inferred type is not suitable, perhaps because you need a more general type, you can provide one explicitly or even typecast the right-hand side:
class Person var _name = '' to dynamic var _age as int64 = 0 def foo i = 0 to int64 j as int64 = 0
Sometimes you want the variable to be typed as a base class because you may assign other things to it in the future:
shape = Circle() to Shape share as Shape = Circle()
See also: AdditionalDoc, TypesOverview