method mirrors

46 views
Skip to first unread message

Anders Holmgren

unread,
Jul 25, 2014, 10:06:31 PM7/25/14
to mi...@dartlang.org
I'm trying to add support for adding constraints to function parameters and returns for the constrain library.

For objects I look up and cache the constraint metadata based on the object's Type. For functions I'm struggling to find what to key the cached meta data on.

I want to support standalone functions like

String bar(int blah, [String boo = 'abc']) => '';

and methods on classes like

class Foo {
 
String bar(int blah, [String boo = 'abc']) => '';
}

So the idea is to allow things like


@NotNull String bar(@Min(10) int blah, [@Pattern('[\w]+') String boo = 'abc']) => '';

and methods on classes like

class Foo {
 
@NotNull String bar(@Min(10) int blah, [@Pattern('[\w]+') String boo = 'abc']) => '';
}


So I need a unique key for these functions / methods such that:

  • it is always the same for the same function
  • it is always the same for the same method of a class. i.e. across all instances of that class / subclasses etc
  • functions with the same signature (runtimeType) are seen as distinct i.e. don't match. e.g. the function bar does not match the method bar on the class Foo
The only thing that I have found that seems to fit this bill is MethodMirror.

However, MethodMirror is part of the mirror library which I plan to move away from at some point and use code generation instead. For objects I can use Type (via o.runtimeType). Is there something similar I can use here that I am missing?

A

Günter Zöchbauer

unread,
Jul 26, 2014, 8:58:26 AM7/26/14
to mi...@dartlang.org
Have you seen this discussion? https://code.google.com/p/dart/issues/detail?id=144
ab bit old but seems about a similar requirement I suppose

Anders Holmgren

unread,
Jul 26, 2014, 8:15:16 PM7/26/14
to mi...@dartlang.org
Thanks Günter. Interestingly the first issue is marked as won't fix but clearly works now

  print(bar == bar); // = true
  print(obj.bar == obj.bar); // = true
  print(new Foo().bar == new Foo().bar); // = false


All as I'd expect.

My issue is that I want something I can refer to such that 

print(new Foo().bar.thingy == new Foo().bar.thingy); // = true

but I don't think there is such a thingy in this case.

  print(reflect(new Foo().bar).function == reflect(new Foo().bar).function); // = true


works but relies on runtime mirrors
Reply all
Reply to author
Forward
0 new messages