Why the result of 1 + "1" is "11"

8 views
Skip to first unread message

江添亮

unread,
Oct 23, 2011, 11:49:26 AM10/23/11
to General Dart Discussion
Why the result of evaluation: 1 + "1 ", is "11"?
I can understand "1" + 1 is evaluated to "11".
The document of String said so.
but why 1 + "1" is "11"?
spec said, e1 op e2 is evaluated as e1.op(e2).
So 1 + "1" invokes a method operator + from the type of 1,
(which is I guess some implementation defined class that isn't exposed
to the users.)
Interface num took a parameter as static type num and returns num.
with no document of how it works.
But I assume if it use static type num in an official document, it
doesn't expect a type that isn't a sub type of num.

Florian Loitsch

unread,
Oct 23, 2011, 12:24:48 PM10/23/11
to 江添亮, General Dart Discussion
This is a limitation of DartC (the compiler that is used for the Dartboard): in order to get decent performance DartC is allowed to optimize under the assumption that checked mode doesn't throw any AssertionError. In particular this means that the type-check for num.+ does not throw.
In your example this assumption is violated and the result is thus undefined.
There is already a similar bug open: http://code.google.com/p/dart/issues/detail?id=208
// florian

--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

江添亮

unread,
Oct 23, 2011, 12:46:28 PM10/23/11
to General Dart Discussion


On Oct 24, 1:24 am, Florian Loitsch <floit...@google.com> wrote:
> On Sun, Oct 23, 2011 at 17:49, 江添亮 <boost...@gmail.com> wrote:
> > Why the result of evaluation: 1 + "1 ", is "11"?
> > I can understand "1" + 1 is evaluated to "11".
> > The document of String said so.
> > but why 1 + "1" is "11"?
> > spec said, e1 op e2 is evaluated as e1.op(e2).
> > So 1 + "1" invokes a method operator + from the type of 1,
> > (which is I guess some implementation defined class that isn't exposed
> > to the users.)
> > Interface num took a parameter as static type num and returns num.
> > with no document of how it works.
> > But I assume if it use static type num in an official document, it
> > doesn't expect a type that isn't a sub type of num.
>
> This is a limitation of DartC (the compiler that is used for the Dartboard):
> in order to get decent performance DartC is allowed to optimize under the
> assumption that checked mode doesn't throw any AssertionError. In particular
> this means that the type-check for num.+ does not throw.
> In your example this assumption is violated and the result is thus
> undefined.
> There is already a similar bug open:http://code.google.com/p/dart/issues/detail?id=208

So it's a limitation of the DartC.
Thank you.

Pimm Hogeling

unread,
Oct 23, 2011, 3:37:15 PM10/23/11
to General Dart Discussion
Just to clarify: as Florian kind-of mentioned, your example would raise a warning checked mode. In plain English: your example does not work in checked mode.

You can verify this yourself: try.dartlang.org/s/BWgd

By not testing your code in checked mode, you are ignoring any warnings that might have been raised during such a test.

2011/10/23 江添亮 <boos...@gmail.com>
Reply all
Reply to author
Forward
0 new messages