Ambiguous reference when getter/setter have differ

2019-08-08 15:21发布

问题:

The following code raises an ambiguous reference to value at compile time:

import flash.display.Sprite;

public class Main extends Sprite
{
    private var _value : Number = 0.;

    public  function get value() : Number           { return _value; }
    private function set value(v : Number) : void   { _value = v; }

    public function Main() : void
    {
        value = 42.;
    }
}

I suspect some kind of bug in the compiler, though I didn't actually read the ECMA standard.

Before someone asks those questions:

  • Private setters do make sense.
  • The ambiguity also exists with custom namespaces (which is the problem I'm facing).

回答1:

it is indeed a bug in the compiler, and it is listed in the bugs. its stated that its an oversite of the developers and wont be fixed any time soon.

if you are needing to specifically run a function to set privately (rather than just assign the value, in which case you can leave out the setter function completely and itll run) then you will have to run a seperate function as Sandro said.



回答2:

I think this may be a limitation of AS3. You could create a private function called setValue() or if your set on having a setter you might be able to get away with this, although it's not very pretty.

package {
    import flash.display.Sprite;

    public class Main extends Sprite {
        private var __value :Number = 0;

        public function Main(): void {
            _value = 42;
        }
        public function get value():Number {
            return __value;
        }
        private function set _value(v:Number):void {
            __value = v;
        }
    }
}