noSuchMethod, warnings and minification

95 views
Skip to first unread message

Chris Buckett

unread,
Apr 8, 2012, 2:47:15 PM4/8/12
to General Dart Discussion
Whilst writing a blog post today around noSuchMethod, I came across a
couple observations:

Assuming I have a class with noSuchMethod, as follows:
class Person {
noSuchMethod(f , args) {
if (f == "get:name") {
return "Chris";
}
}
}

elsewhere:
var p = new Person();
print(p.name);

I have two observations:
1. The tools will always give a warning on p.name
2. Any minification process will need to know not to minify the .name
part of p.name

Questions:
1. Could there be some way to indicate that a class is actively
handling noSuchMethod (such as the presence of an overridden
noSuchMethod() method), and not produce a warning,
or
Is there a way to suppress these warnings?
(Another solution might be to expect classes that handle noSuchMethod
to implement a specific interface rather than having noSuchMethod in
the base Object)

2. Would this code be expected to run fine in checked mode?

3. How would minification handle using noSuchMethod (especially
minification of javascript)?

Cheers,
Chris

--------------------
http://dartwatch.com

Ladislav Thon

unread,
Apr 8, 2012, 3:02:51 PM4/8/12
to Chris Buckett, General Dart Discussion
Questions:
1. Could there be some way to indicate that a class is actively
handling noSuchMethod (such as the presence of an overridden
noSuchMethod() method), and not produce a warning,
or
Is there a way to suppress these warnings?
(Another solution might be to expect classes that handle noSuchMethod
to implement a specific interface rather than having noSuchMethod in
the base Object)

2. Would this code be expected to run fine in checked mode?

3. How would minification handle using noSuchMethod (especially
minification of javascript)?

In the next-to-last slide of this presentation https://plus.google.com/u/0/109866369054280216564/posts/13QYRogVF7S, Gilad mentions that there will be a mechanism to selectively protect against minification. This was about reflection, but I think it applies to noSuchMethod too.

LT

Chris Buckett

unread,
Apr 8, 2012, 3:06:19 PM4/8/12
to Ladislav Thon, General Dart Discussion
Yes, that was what made me think of this in the context of noSuchMethod.  I'm looking forward to finding out more about this.
Cheers,
Chris.

Gilad Bracha

unread,
Apr 8, 2012, 9:56:15 PM4/8/12
to Chris Buckett, General Dart Discussion
Hi Chris,


On Sun, Apr 8, 2012 at 11:47 AM, Chris Buckett <chrisb...@gmail.com> wrote:
Whilst writing a blog post today around noSuchMethod, I came across a
couple observations:

Assuming I have a class with noSuchMethod, as follows:
class Person {
 noSuchMethod(f , args) {
   if (f == "get:name") {
     return "Chris";
   }
 }
}

elsewhere:
var p = new Person();
print(p.name);

I have two observations:
1. The tools will always give a warning on p.name

No it won't.  As written, p is dynamically typed and no warning should be issued. If you change this to

Person p = new Person();
print(p.name); 

You will get a warning. This makes sense, because we can't tell what methods noSuchMethod() will handle., e.g.,

p.vaporizeUniverse(); // might not work

In some situations, you might know the interface you are implementing by means of noSuchMethod, for example

class TLogAndForwardimplements T {
  T myT;
  noSuchMethod( ...) {
    // log the call, then forward it to myT
  }

Now you would get some warnings at the point of declaration complaining that you don't implement T, but your uses would not be flagged by the type checker.

The overall question of how to tell the type checker to just shut up about a specific issue remains open. I think we should do something on that front as well, but we're not there yet.

2. Any minification process will need to know not to minify the .name
part of p.name

As long as name is being called in your program, you don't have a problem. I don't expect us to obfuscate names, just minify them.  Reflection poses harder issues, and I expect we'll find a way to get around that. For example, have a list of symbols that indicate things we want to protect against being optimized out; such a list could be fed to a deployment tool that does the actual minification.

 

Questions:
1. Could there be some way to indicate that a class is actively
handling noSuchMethod (such as the presence of an overridden
noSuchMethod() method), and not produce a warning,
or
Is there a way to suppress these warnings?
(Another solution might be to expect classes that handle noSuchMethod
to implement a specific interface rather than having noSuchMethod in
the base Object)

2. Would this code be expected to run fine in checked mode?

Yes.  

3. How would minification handle using noSuchMethod (especially
minification of javascript)?

Cheers,
Chris

--------------------
http://dartwatch.com



--
Cheers, Gilad

Chris Buckett

unread,
Apr 9, 2012, 10:23:48 AM4/9/12
to General Dart Discussion
Thanks Gilad for the clarifications.

I forget that in Dart, I can use an interface (or even a class) even
when what I'm getting instantiated isn't something explicitly
implements that interface.

Cheers,
Chris.

On Apr 9, 2:56 am, Gilad Bracha <gbra...@google.com> wrote:
> Hi Chris,
>

Chris Buckett

unread,
Apr 9, 2012, 10:33:19 AM4/9/12
to General Dart Discussion
Actually, that shows something interesting (may be a bug in the
DartVM).
The following code: ( http://try.dartlang.org/s/DnY3 )
has an interface Person extending a class JsonObject (which AFIK a
class can be used as an interface)

main() {
Person p = new JsonObject();
print(p.name);
}

interface Person extends JsonObject {
String name;
}

class JsonObject {
noSuchMethod(f,args) {
if (f == "get:name") {
return "Chris";
}
}
}

In dartboard, this gives an error about noSuchMethod (which I think
isn't working in dartboard), however, in the editor, I get an error:
"class 'JsonObject' is used where an interface is expected"

Would I expect this to work? - If so, I'll log a bug.

Cheers,
Chris.

Gilad Bracha

unread,
Apr 9, 2012, 10:38:31 AM4/9/12
to Chris Buckett, General Dart Discussion
Per spec, it should work, but it isn't implemented yet. Please file a bug.
--
Cheers, Gilad

Chris Buckett

unread,
Apr 9, 2012, 10:53:59 AM4/9/12
to General Dart Discussion
Thanks for the confirmation.
Logged: http://dartbug.com/2496

On Apr 9, 3:38 pm, Gilad Bracha <gbra...@google.com> wrote:
> Per spec, it should work, but it isn't implemented yet. Please file a bug.
>

Eric Ayers

unread,
Apr 9, 2012, 10:56:49 AM4/9/12
to Gilad Bracha, Chris Buckett, General Dart Discussion
When you file the bug, will you clarify what you mean by 'in the editor'.  I mean do you mean you get this error in the list of problems in the Editor UI, generating JavaScript or after you launch in Dartium?
--
Eric Ayers | Software Engineer | zun...@google.com | +1 404 487 9229

Chris Buckett

unread,
Apr 9, 2012, 11:01:01 AM4/9/12
to Eric Ayers, Gilad Bracha, General Dart Discussion
Sure, no problem.

Cheers,
Chris.

Matthias Hausner

unread,
Apr 9, 2012, 11:42:47 AM4/9/12
to Gilad Bracha, Chris Buckett, General Dart Discussion
As Gilad says, the VM does not implement this feature yet. I merged your bug into issue 1971.

  Matthias

Eric J. Smith

unread,
Apr 9, 2012, 12:00:48 PM4/9/12
to mi...@dartlang.org
This is pretty awesome.  When I saw that there was a noSuchMethod feature, I was hoping someone would implement a JSON helper class like this.  Nice work!

Chris Buckett

unread,
Apr 9, 2012, 2:50:35 PM4/9/12
to General Dart Discussion
Thanks all for your help with this.
Cheers,
Chris.

On Apr 9, 4:42 pm, Matthias Hausner <haus...@google.com> wrote:
> As Gilad says, the VM does not implement this feature yet. I merged your
> bug into issue 1971.
>
>   Matthias
>
>
>
>
>
>
>
> On Mon, Apr 9, 2012 at 7:38 AM, Gilad Bracha <gbra...@google.com> wrote:
> > Per spec, it should work, but it isn't implemented yet. Please file a bug.
>
Reply all
Reply to author
Forward
0 new messages