Symbol with private identifier argument

倖福魔咒の 提交于 2019-12-04 16:19:53

If it doesn't throw then the VM has a bug in the const Symbol constructor.

The problem is that "_s" does not identify a private variable without also saying which library it belongs to. The symbol constructor has a second argument taking a LibraryMirror for that reason, and passing in a private name without also passing in a mirror should throw. That's hard to do in a const constructor without side-stepping the requirements of a const constructor (no executing code!), which is likely why the VM doesn't handle it. It needs to be special-cased at the compiler level.

You will also find that const Symbol('_s') is not the same as #_s. The latter creates a private symbol for the current library, the former (if it runs) creates a non-private symbol with the name '_s', which is not really useful. For example print(identical(#_s, const Symbol('_s'))); prints false.

The To get hold of the symbol I think you would need to get it from the object. e.g.

  reflect(thing).type.declarations.keys.firstWhere(
    (x) => MirrorSystem.getName(x) == "_privateThingIWant");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!