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?

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


I can understand logically why the first compiles (in the older compiler). When considering an instance of the object, getting the obj.func property should return the member function you've defined, while setting the obj.func property should call the setter you've defined. This doesn't seem to be an ambiguity to me, though as we've seen, the runtime disagrees with me.

In the second case, you've defined a var (which defaults to the internal scope since you didn't say public, but that's another story) which, being externally visible, implicitly defines a getter and setter. So if someone sets the obj.someVar44 property of your object, are they calling the setter or setting your variable value? It's clearly an ambiguity and a duplicate definition.


Need Your Help

How to integrate Google Plus and Facebook Authentication together in the same activity of Android project?

android facebook google-plus

I need to to integrate both Facebook and Google Plus in the same activity but I'm not getting success in it. I have tried both in individual projects and they work fine but when I add both projects

Class design help needed

design-patterns ooad

I need to develop a certain sw module which outputs the data in the following format Main object and related object and Quantity