Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this property/function name collision compile in AS3?

In ActionScript 3.0, this compiles:

public function set func(value:Function):void
{

}

public function func():void
{

}

This doesn't:

public function set someVar44(value:int):void
{

}

var someVar44:int;

Why does the first one compile? I suppose it's possible that Adobe just specifically and arbitrarily decided to block this for variables and to allow it for functions, but allowing it for either functions or variables doesn't seem to make any sense. I'm suspicious there's more to the story here. What am I not seeing?

like image 728
Panzercrisis Avatar asked Dec 30 '25 20:12

Panzercrisis


1 Answers

This is really interesting, and took a fair amount of digging to get down to (although the answer seems painfully obvious).

As you know variables/properties cannot be declared in the same scope with identical names. Therefore the set function someVar44() and the variable someVar44 are in direct conflict (besides issues with trying to initialize the variable twice). Conversely if you had tried:

public function get func(value:Function):void
{

}

you would have ran into a similar issue with a duplicate function definition error. So why does the set function seem to allow you to get past these errors? As setters and getters are known for accessing and mutating properties of a class, it would seem they are also treated as class properties as opposed to a typical method, but this is not entirely the case. In fact, only the setter appears as a property of the public interface, the getter on the other hand is a method that can read like a property.

The setter:

public function set func(value:Function):void

Is read exactly like a property of the object, and without any other properties in direct conflict with it (i.e. - there is no current property like var func.) you do not receive a compiler error.

From adobe:

set Defines a setter, which is a method that appears in the public interface as a property.

get Defines a getter, which is a method that can be read like a property.

I believe that is why you are not getting a compiler error with set method. Although if you attempt to access that set method, the priority is immediately assumed to the function func(). That is, if you attempt this.func = function():void { } you will get the error:

Error #1037: Cannot assign to a method func

like image 182
Bennett Keller Avatar answered Jan 01 '26 09:01

Bennett Keller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!