简体   繁体   中英

dart: proxy annotation usage

Documentation for the @proxy annotation states:

If a class is annotated with @proxy , or it implements any class that is annotated, then the class is considered to implement any interface and any member with regard to static type analysis. As such, it is not a static type warning to assign the object to a variable of any type, and it is not a static type warning to access any member of the object.

However, given the following code:

import 'dart:mirrors';

@proxy
class ObjectProxy{
  final InstanceMirror _mirror;

  ObjectProxy(Object o): _mirror = reflect(o);

  @override
  noSuchMethod(Invocation invocation){
    print('entered ${invocation.memberName}');
    var r = _mirror.delegate(invocation);
    print('returning from ${invocation.memberName} with $r');
    return r;
  }
}

class ClassA{
  int k;

  ClassA(this.k);
}

void printK(ClassA a) => print(a.k);

main() {
  ClassA a = new ObjectProxy(new ClassA(1)); //annoying
  printK(a);
}

dart editor warns

A value of type 'ObjectProxy' cannot be assigned to a variable of type 'ClassA'.

The code executes as expected in un-checked mode, but the warning is annoying, and from what I can tell, suppressing that warning is the only thing the @proxy tag should be doing.

Am I misunderstanding the usage of the @proxy tag, or is this a bug with dart editor/analyzer?

What you can do is

@proxy
class ObjectProxy implements ClassA {
  final InstanceMirror _mirror;

  ObjectProxy(Object o): _mirror = reflect(o);

  @override
  noSuchMethod(Invocation invocation){
    print('entered ${invocation.memberName}');
    var r = _mirror.delegate(invocation);
    print('returning from ${invocation.memberName} with $r');
    return r;
  }
}

you need to declare that ObjectProxy implements an interface but you don't have to actually implement it. If you think this is a bug report it at http://dartbug.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM