开发者

Why write functions :void?

开发者 https://www.devze.com 2023-02-07 15:41 出处:网络
I am in the habit of writin开发者_如何学编程g: function functionName():void { } If my function has no return type (\":void\"), however, I notice that my function will work if I write:

I am in the habit of writin开发者_如何学编程g:

function functionName():void
{

}

If my function has no return type (":void"), however, I notice that my function will work if I write:

function functionName()
{

}

...without specifying the return type. Why is it considered good form to show the return type as :void?


Because it follows the strict use of data typing, if there is a :void return type, the compiler could warn on non void returns. (although the standard compiler does not.)

Without it the function declaration is semantically ambiguous.

(edit: updated for clarification, code is for us humans to be able to read, if readability and semantic richness were not all that important, we'd all be coding in binary, using a flip switch.)


The long and short of it is that every object in Flash has a data type. This can be a native data type, like "String" or "Number" or "Array" or "int", or it can be a custom data type, like "MyCustomClass" or "ICustomInterface".

If Flash knows the data type of an object, it's able to work with it much more quickly because it knows exactly what methods and properties that object does or does not have. This is why, for instance, you can iterate through a Vector much more quickly than you can iterate through an Array - a Vector forces every element to be of the same type, which means that the runtime doesn't have to sit there and type-check every single item in your Vector. It just knows, for instance, that every item in the vector implements the IWhatever interface, and that's all it cares about.

So, this is where :void comes in. In AS3, functions are also objects. A function can return absolutely anything - so you assign a type declaration to the function to tell the Flash Player that when an object is returned from that function it is of a particular type.

There's another reason, which has been touched on above. When you declare a return type, your function is type safe. If you declare :int and try to return a String, you'll get an error. This is good - that error tells you that your code is not behaving the way you expect it to.

When we declare functions to be :void, then, what we're telling flash is that no return is expected from this function. If the function should happen to return anything at all, it'll throw an error.

I hope that helps! :)


In the former, this would cause an error:

var expectedValue:String = functionName();

Whereas in the latter the above would go undetected and may instead lead to a runtime error (which is arguably harder to find and fix).

i.e. static typing helps you help the compiler help you find errors in your code.


It's not "good form to write :void" it's just a practice from C-like languages where functions must always return a type.

Some languages, like Pascal do not need that definition (void) HOWEVER, in Pascal, a return-less function is called a "procedure". Example:

procedure sayHello();
begin
  writeln("Hello, from pascal!");
end;

function addTogether(a, b: integer);
begin
  Result:=a+b;
end;

Edit: Languages like PHP also do not specify what to return, in which case the default is (wonder of wonders...) null. Critics would argue that PHP does not have a type system so a full function signature may not be needed. I'd give you a point for that, but the issue still holds, no return type is easily assumed to not expect one, or a value of null.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号