actionscript-3 - 包名称与 getter 和 setter 冲突?

标签 actionscript-3 compiler-construction package

各位。所以,我不久前就遇到了这个编译错误。由于有一个简单的修复方法,而且当时我没有找到任何相关的内容,所以我最终放弃了它。

我刚刚想起了它,现在我想知道这是否真的是语言语法的一部分(我对此非常怀疑),或者它是否是一个编译器错误。我对此纯粹是好奇——它并不会真正影响开发,但很高兴看看你们中是否有人已经看到了这一点。

package view {
    import flash.display.Sprite;

    public class Main extends Sprite {
        private var _view:Sprite = new Sprite();

        public function Main() {
            this.test();
        }

        private function test():void {
            trace(this.view.x, this.view.y);
            //1178: Attempted access of inaccessible property x through a reference with static type view:Main.
            //1178: Attempted access of inaccessible property y through a reference with static type view:Main.
            //Note that I got this due to the package name.
            //It runs just fine if I rename the package or getter.
        }

        public function get view():Sprite {
            return this._view;
        }
    }
}

最佳答案

我想说这要么是编译器错误,要么是 spec 中的不一致。 .

引用第 11.1 章包命名空间(我会直接链接,但文档使用框架):

Packages exist only at compile time. The static existence of packages allows us to give them certain properties that would not be possible if they could be manipulated at runtime. In particular:

Package names may have embedded dots. Fully qualified package references may and must be expressed using the dot operator rather than the usual :: syntax for qualified names.

But because there is no runtime value for a package name, packages cannot be aliased or otherwise used in an expression that uses a runtime value.

When encountered in a valid context by the compiler, the meaning of a package name becomes fixed; any interpretation at runtime is no longer possible.

For this reason, a package name always shadows locally defined names, independent of the scope chain, when that package name is used on the left hand side of a dot operator.

现在,从上面的内容来看,我推测这一行:

trace(this.view.x, this.view.y);

编译器不应将其解释为引用 view 包,因为它似乎与这一点相矛盾 - 我将其称为 A):

packages cannot be aliased or otherwise used in an expression that uses a runtime value

因为this,除非我弄错了,是一个运行时值。

然后,如果你使用 this ,歧义可以作为你的 getter 来解决,我认为,但根据本段 - 让我们称之为 B),它不会:

For this reason, a package name always shadows locally defined names, independent of the scope chain, when that package name is used on the left hand side of a dot operator.

因此,如果您不使用 this,从规范中可以清楚地看出 view.x 应解释为对 x 定义的引用view 包中。

如果你明确地说this,那么在我看来,A) 和 B) 之间存在矛盾。根据A)不应存在混叠;但似乎正在发生别名,因为点运算符的左侧使用了一个包名称。所以我的猜测是编译器没有在上下文中解析包,可以这么说,只是检查点运算符左侧的任何名称是否与定义的包的名称匹配。

关于actionscript-3 - 包名称与 getter 和 setter 冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3033675/

相关文章:

flash - AS3 - 如何找到对象相对于舞台的位置?

javascript - Flash JavaScript 界面

flash - 是否可以使用 actionscript 3 中的 mailto 函数向邮件添加附件?

python - 让 python 3.4 找到我的自定义包

perl - 为什么符号的封装限定会导致使用更少的内存,即使符号是本地导入的?

npm - 我应该提交yarn.lock 和package-lock.json 文件吗?

actionscript-3 - 游戏开发中的 HTML 与 ActionScript3?

Java:如何访问 try-catch 循环中的赋值?

c - 需要大型 C99 测试文件

C 到 MIPS 转换