dart: proxy annotation usage

Issue

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?

Solution

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

Answered By – Günter Zöchbauer

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *