Groups | Blog | Home
all groups > flash actionscript > march 2005 >

flash actionscript : Strong typing and dynamic variables


Whittaker007
3/15/2005 9:42:57 PM
Hi there, I am using AS2 and strong typing for most of my projects these
days, but I find that I occasionally need a variable type to be dynamic when
sent as a parameter in a function. For example I have a function called
getAttributeName which takes an object and a variable as parameters. The
function iterates through the attributes of the object and returns the first
attribute name whose value equals the variable. However the variable (the value
of the attribute) can be anything at all, and it makes no sense to limit the
function to only search for one type of variable. So my function declaration
looks like this: function getAttributeName (obj:Object, val) { ... } However I
would much prefer to explicitly declare the type of val as being dynamic. For
example function getAttributeName (obj:Object, val:Dynamic) { I take it that
this is not (yet) catered for with strong typing in Flash? There are an awful
lot of types which you can choose in the code hint dropdown list that appears
after typing the colon after the variable name. Dynamic is not one of them, but
I take it that it is not one of the others in the list with a name I didn't
expect? Thanks, Scott
Byron Canfield
3/16/2005 1:00:55 PM
The lack of specific type is what allows it to be dynamic, and note also
that typing of objects is not a requirement -- it only enables the error
checking at compile time.

--
--------
Reality will not be altered to comply with preconceived notions.

Byron "Barn" Canfield
Flash example files: http://www.canfieldstudios.com

Byron Canfield
3/16/2005 3:08:05 PM
Since, in Flash, the data typing is only a means for the compiler to detect
data type mismatches, what would be the point in having a type that allows
any data type?

--
--------
Reality will not be altered to comply with preconceived notions.

Byron "Barn" Canfield
Flash example files: http://www.canfieldstudios.com

Byron Canfield
3/16/2005 5:44:52 PM
You can accomplish alerting readers of the code far better by using a proper
naming convention, which serves further to indicate the intended datatype
EVERYWHERE the name appears, rather than just at the initial declaration. It
is the avoidance of a proper naming convention that I would label "lasy
programming."

--
--------
Reality will not be altered to comply with preconceived notions.

Byron "Barn" Canfield
Flash example files: http://www.canfieldstudios.com

Whittaker007
3/16/2005 10:16:49 PM
Yeah, that's what I figured. It defies my sense of order though, and looks
like it could easily be an oversight rather than a necessary omission. I would
like to see an explicit polymorphous type declaration, and even multiple type
assignments to allow values only for a range of types. e.g. function myFunc
(stringVar:String, anyVar:Dynamic, multiVar:Object:Array:MovieClip) {...}
Whittaker007
3/16/2005 11:54:39 PM
What would be the point? Completeness. Symmetry. A visual flag for programmers
which alerts code readers to the fact that the variable is necessarily
polymorphous and not simply lazy programming. Like comments, a piece of code
doesn't have to affect the way the program works to have an intrinsic
syntactical value.
Jeckyl
3/17/2005 8:42:40 AM
To allow any type of value, you simply do NOT specify the type eg

getAttributeName (obj : Object, val ) {

In this case, the 'obj' must be of class Object (or derived from it) and the
'val' can be anything at all (it is untyped)
--
Jeckyl

Jeckyl
3/17/2005 9:26:33 AM
I've never seen a language that allows you to specify multiple types for a
variable.

And having no type IS EXACTLY a polymorphous type .. it is an indication to
the compiler that the variable is allowed to contain any type of value.

NOTE: Unlike JavaScript, the typing is only a compiler hint, and does NOT
make the variable typed (in that it does not mean you can only store one
type of value in the variable, nor that values are converted to that type
when assigned). It is not really strong typing at all .. just type hinting.
So don't get caught into thinking using types really makes you code type
safe ... it helps, but is nowhere near perfect.
--
All the best
Jeckyl

Jeckyl
3/17/2005 11:11:36 AM
You can use 'Object' for exactly what you want then eg

var myvar : Object

because Object is the base class of everything .. eg
var y : Object = 123;
var z : Object = "string";
are both fine

NOTE: There is a difference between a type of Object and no type at all eg

var x : String;
var y = 123; // untyped
var z : Object = "string";
x = y; // works
x = z; // fails

I'll explain why x=y works and x=z fails above if anyone cannot see the
reason. And note that in the above you can change it like this...
x = String(z); // works
--
All the best
Jeckyl

AddThis Social Bookmark Button