google / reflectable.dart

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.

Home Page:https://pub.dev/packages/reflectable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot access private name _PageWidget ?

moxiaohao opened this issue · comments

How can I access private name _Class?
I want generate reflectable.dart for StatefulWidget StatelessWidget , it can be done ?

No, unfortunately there is no way an approach which is based on code generation can violate Dart privacy (and reflectable is based on code generation, it doesn't have access to any special primitives). Reflectable generates the code that supports the reflective behavior in a single output library which is (normally) imported by the entry point of the program (that is, your 'main.dart'). The generated library can import any other library which is directly or indirectly reachable from from the entry point, but it cannot access any private names in any of those libraries. So, just like any code that you could write yourself, you cannot access a private name in another library.

Wouldn't you be able to restrict the support for reflection in such a way that it avoids including those private declarations?

No, unfortunately there is no way an approach which is based on code generation can violate Dart privacy (and reflectable is based on code generation, it doesn't have access to any special primitives). Reflectable generates the code that supports the reflective behavior in a single output library which is (normally) imported by the entry point of the program (that is, your 'main.dart'). The generated library can import any other library which is directly or indirectly reachable from from the entry point, but it cannot access any private names in any of those libraries. So, just like any code that you could write yourself, you cannot access a private name in another library.

Wouldn't you be able to restrict the support for reflection in such a way that it avoids including those private declarations?

Thanks for the reply Erik, and I got another problem here.
I want to make an instance for Map by reflectable and how can I specify the two Generics?

I want to make an instance for Map by reflectable and how can I specify the two Generics?

This is again touching upon a limitation of reflectiable: There is no way you can turn an instance of Type into a type in a Dart program, except by equality tests on Type instances. For example:

void main() {
  Type t = int;
  var map;
  // map = <String, t>{}; // Error, `t` is not a type, it is a `Type`.
  switch (t) {
    case String: map = <String, String>{ 'foo': 'bar' }; break;
    case int: map = <String, int>{ 'foo': 16 }; break;
    case double: map = <String, double>{ 'foo': 1.6 }; break;
    default: throw "Can't recognize the type $t!";
  }
  print(map['foo']);
}

The type Type is relevant here because it is the most likely representation of a type in code using reflection (it could also be a TypeMirror, but they are even further away from being usable as a type).

I'd recommend that you try to use techniques similar to dependency injection for this purpose, which is basically the same thing as the switch statement above, with a lot of software engineering tricks added in order to make it more user-friendly and scalable.

It would be possible to provide access to the type mirrored by a TypeMirror, cf. #252, and it shouldn't be too difficult to support that feature.

There's a trade-off between the demands on space resources and the expressive power of this feature, because it will make the generated code larger.