3

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
DomJack
  • 4,098
  • 1
  • 17
  • 32

1 Answers1

0

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Not quite what I'm looking to do (I want it to be able to proxy any object, including some not known at compile time), but I suppose I'll have to settle for that. Thanks. – DomJack Mar 20 '15 at 01:37