PDA

View Full Version : Type declarations



nikomax
February 6th, 2009, 01:52 PM
I have been learning Java for my uni course and to me it seems that AS3 is similar to it in a few ways.

But how strict is the type definition in AS3? For those of you that don't understand here is an example.

JAVA


Boolean _myVar = false;


public Boolean myFunction(){
return _myVar;
}
AS3


var _myVar:Boolean = false;


public myFunction():Boolean{
return _myVar;
}
You can't compile Java code with out the types defined for variables and the return type defined for functions.

However in AS3 I seem to be able to miss out these type definitions without out any difference being made or even half-heartedly do it leading to messy looking code.

So do I really need to use type declarations all the time or is it there just for my piece of mind?

Krilnon
February 6th, 2009, 03:17 PM
You don't ever need to use them. You could say that they are there for your piece of mind, but people extend that argument to say that it helps them to avoid code errors and make their code clearer.

I guess one of the main reasons that some people type their data is that it improves the efficiency of certain operations. For example, it's faster to do integer multiplication than floating point multiplication. I doubt it's worth anyone's time to enumerate all of the cases in which data typing can improve efficiency, so hopefully you see the point.

creatify
February 6th, 2009, 03:22 PM
In my personal opinion, part of the reason that missing type declarations won't throw an error during the compile, is: unlike with many other programming languages, there are a ton of creatives that use Flash, non-developers. AS1 was a cinch for people to just get things to work without hardly any errors - that's not the case with AS3, but there are still few loopholes that allow things to be made with a less-strict approach, since they really won't effect the outcome of the finished piece.

That said, you should always use them.

Krilnon
February 6th, 2009, 03:46 PM
That said, you should always use them.

I feel like I should point out that whether or not one should declare types is a pretty debatable matter of opinion as a feature of languages in general. There's quite a bit of "controversy" between proponents of different languages that support only one of the two. However, you can do either in ActionScript, so I would suggest that you should choose whether or not you're going to declare types based on your personal preference, who (if anyone) you're going to be sharing your code with, and how your choice will modify your ability to achieve whatever goals you've set with your application.

Even performance, which seems like a clear choice when type declarations are concerned, might not be an easy decision to make. If you have a limited amount of time to code your project and you're more comfortable writing untyped code, you might be able to use the time you save to optimize your algorithms. In that case, you might find some way to rewrite some exponential running time code to be polynomial, for instance.

Note: In practice, I don't think that it actually takes any significant amount of time to type all of most of the variables in a given project, so it's probably not a big deal to code with them if you don't have a strong preference against using them. :P

senocular
February 6th, 2009, 04:20 PM
... they also give your editing tool information on what to provide for code hints
FTW!

theCodeBot
February 6th, 2009, 04:22 PM
Just a side note, flex will throw warnings, but no errors, when it sees that you're not compiling strictly.

Though speed is a minor factor, strong typing has advantages in code clarity and preventing possible errors. For example, with AS3, function arguments have a declared type:



function doubleString(toFix:String,tackOn:int):String {
for(var i=0;i<tackOn;i++) {
toFix+= toFix;
return toFix;
//Yes, I know, you shouldn't EVER see a function as retarded as this one IRL.
}
}

If you were to not declare the argument types, and, say, someone passed an object where the string goes, it'll be errors ahoy. So, we type cast things moreover just to reduce errors.

Krilnon
February 6th, 2009, 04:49 PM
... they also give your editing tool information on what to provide for code hints
FTW!

That's true, but there are cool ways to do code hinting without type declarations. For example, the DLTK has code hinting for dynamic languages like Python and Ruby: http://www.eclipse.org/dltk/


Just a side note, flex will throw warnings, but no errors, when it sees that you're not compiling strictly.

Though speed is a minor factor, strong typing has advantages in code clarity and preventing possible errors. For example, with AS3, function arguments have a declared type:



function doubleString(toFix:String,tackOn:int):String {
for(var i=0;i<tackOn;i++) {
toFix+= toFix;
return toFix;
//Yes, I know, you shouldn't EVER see a function as retarded as this one IRL.
}
}

If you were to not declare the argument types, and, say, someone passed an object where the string goes, it'll be errors ahoy. So, we type cast things moreover just to reduce errors.

Maybe you just picked a bad example, but if you don't type that function, you can actually do some more flexible things than if you had typed it. For example, you can rely toString to make the += assignment work for Numbers, Arrays, Points, etc.:
function doubleString(toFix,tackOn) {
for (var i=0; i<tackOn; i++) {
toFix+= toFix;
return toFix;
}

}

trace(doubleString(new Point(4, 3), 1)); // (x=4, y=3)(x=4, y=3)

The complaint about the lack of a return that you would get if you had declared a return type (other than void) even disappears. Of course, the loop behavior is perhaps not what you'd expect, but it's what you would expect if you look at the code moderately closely.

Certainly, certain kinds of errors can be reduced by declaring types in your code. However, I've seen some fairly convincing arguments that point out that there are new kinds of errors and annoyances that come about as a result of typing. To say that one style (typing or not) is unequivocally better than the other seems a bit silly if it's not qualified as just a preference/opinion.

theCodeBot
February 6th, 2009, 07:50 PM
That's true, but there are cool ways to do code hinting without type declarations. For example, the DLTK has code hinting for dynamic languages like Python and Ruby: http://www.eclipse.org/dltk/



Maybe you just picked a bad example, but if you don't type that function, you can actually do some more flexible things than if you had typed it. For example, you can rely toString to make the += assignment work for Numbers, Arrays, Points, etc.: ActionScript Code:

function doubleString(toFix,tackOn) {
&nbsp;&nbsp;&nbsp;&nbsp;for (var i=0; i<tackOn; i++) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;toFix+= toFix;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return toFix;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;
}

trace(doubleString(new Point(4, 3), 1)); // (x=4, y=3)(x=4, y=3)





The complaint about the lack of a return that you would get if you had declared a return type (other than void) even disappears. Of course, the loop behavior is perhaps not what you'd expect, but it's what you would expect if you look at the code moderately closely.

Certainly, certain kinds of errors can be reduced by declaring types in your code. However, I've seen some fairly convincing arguments that point out that there are new kinds of errors and annoyances that come about as a result of typing. To say that one style (typing or not) is unequivocally better than the other seems a bit silly if it's not qualified as just a preference/opinion.

I'd still type the arguments as object ;)

I agree with you wholly as to the flexibility being required, tbh, I just have been built into the mindset. Especially since Flex complains when I don't :P

And yes, I know, crappy example.
I mean, I know that I personally can't think of a use for a repetitive concatenation function...

nikomax
February 7th, 2009, 05:32 AM
So its all down to preference at the end of the day? I guess that is nice to hear for a change. It took me at least a week to get used to Java's strict typing attitude and I remember wishing it was more like AS2 or PHP !!

For now I guess I will stick with the typing to keep me in-line with the Java standard. As crazy as this might sound I spent Thursday basically using AS3 syntax in Java, and wondering why I kept getting compile time errors!

Thanks for clearing this up for me!!