Fabulous Adventures In Coding

Eric Lippert's Blog

Pop Quiz

Every non-trivial language has "gotchas". Here's one. Consider the following JScript, which creates an object and sets some fields to zero:

var x =
{
  0       : 0,
  123     : 0,
  1.23    : 0,
  blah    : 0,
  "dude!" : 0,
  "-1.23" : 0
};

A little weird, but it works just fine. How about this?

var x = { -1 : 0 };

That fails with the message "expected identifier, string or number".

Pop quiz: what the heck is going on here? Surely that is an "identifier, string or number", no?

Published Thursday, July 29, 2004 10:56 AM by Eric Lippert
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Michael Ruck said:

I guess it attempts to create a named field "-1" and set it to 0. However since this is an invalid field identifier, it fails.
July 29, 2004 11:12 AM
 

Eric Lippert said:

Bzzt. Try again. As you can see above, "-1.23" is a valid field identifier. Why should -1 be illegal when "-1.23" is legal?
July 29, 2004 11:18 AM
 

yipyip said:

Negative indices not allowed?
July 29, 2004 11:20 AM
 

Eric Lippert said:

Why not?
July 29, 2004 11:21 AM
 

Anderson Imes said:

It's seems it's evaluating the minus sign as an operator instead of an indication of "below zero". Thus, it's looking for an identifier, string or number before the minus to get the left hand side of the subtraction operation.
July 29, 2004 11:22 AM
 

Larry Osterman said:

Because "-1.23" is a string and -1.23 is a float? Since I'm not a jscript kinda person I'm not sure.
July 29, 2004 11:22 AM
 

Eric Lippert said:

Larry: But in the example above, 1.23 works, so it can't be a float vs string issue.

July 29, 2004 11:24 AM
 

G Man said:

The 1.23 works because it is positive. The "-1.23" works because it is in quotes. I think the people above had it right. I'm just summing up what they said.
July 29, 2004 11:26 AM
 

Neil Mitchell said:

because "-1.23" is a string, and -1 isn't.

My best guess, is that in Javascript 1.23 is a number, and - is a high priority operator. Hence -1.23 is not a number, but an expression. I also found that if +1 is the key, rather than just 1, then it also fails with the same error message.
July 29, 2004 11:28 AM
 

Eric Lippert said:

Anderson got it.

Holy cow, twenty-six minutes from question to correct answer. Don't you people have jobs? :-)

Anything in quotes is a string, and any legal identifier is a legal identifier, but a number preceded by a negative sign is an expression, not a numeric literal.

There is a similar gotcha in VBScript:

print typename(-32767-1) ' Integer
print typename(-32768) ' Long

A short integer can hold any number from -32768 to 32767, so why is the second example a long? Because -32768 is the negative operator applied to a literal long, not a literal short.
July 29, 2004 11:30 AM
 

Larry Osterman said:

Oh, and Eric: I'm trolling blogs from home right now waiting to take daniel to his dress rehearsal :)

That's why I have time for this.

You should look at the time stamps on my "what's wrong with this code" posts - they're scary :)
July 29, 2004 11:42 AM
 

Nicholas Allen said:

Since we're talking about the quirks of numbers in object initializers...

Why doesn't

var x = { 08 : 0 };

whine about being a malformed octal literal?

var y = 08;

does.
July 29, 2004 11:43 AM
 

Eric Lippert said:

I don't get any error for either of those. Or are you talking about JScript .NET?

I talked a bit about malformed octal here:

http://blogs.msdn.com/ericlippert/archive/2003/10/23/53278.aspx
July 29, 2004 11:47 AM
 

Nicholas Allen said:

JScript .NET (jsc 7.10.3052 to be exact)
July 29, 2004 11:49 AM
 

Eric Lippert said:

Ah, how interesting. That's a bug.

I added the warning to the code path where an octal literal is evaluated as part of a numeric expression. Since as we've just seen, a label must not be an expression, the warning is never hit.

In this case we're not looking to evaluate an expression as a number. We need that thing as a canonical string. So we need to turn it into a number, and then turn it back into a string. For some odd reason, the routine that the compiler uses to do that is the same routine as is used at runtime by parseInt, rather than the routine used by the compiler in the main line expression evaluator. parseInt, being a runtime function, doesn't produce compile-time warnings.

I'm not sure why the compiler author made that choice -- I didn't write that part of the compiler.
July 29, 2004 12:01 PM
 

James Mastros said:

For a while, perl had a bug whereby floating point numbers would be interpreted slightly differently depending on where they were used -- it hit very rarely, and only effected the last digit. The problem? Two different functions were being used, one for compile-time constants, one for arithemetic on strings holding floating-point numbers, that did the conversion ever-so-slightly differently. Perl has a much looser compiletime/runtime dichtomy then many languages, and will happily give you warnings at runtime when it feels the need, so that's not an issue there, but the lesson is still important -- stuble differences in behavior aren't generally a good thing, when your users expect two things to work the same way, and if you implement something twice, you'll end up with subtle differences in behavior.
July 29, 2004 12:19 PM
 

Nicholas Allen said:

Interesting. That is kind of weird to use a runtime function to do the conversion.

We check for bad octals in the scanner so the warning has already gone out before anyone gets their hands on the token.
July 29, 2004 12:49 PM
 

Eric Lippert said:

Right -- James has hit the nail on the head. There's only one place in the JScript code that converts strings to numbers, and that's the same code that is used in parseInt/parseFloat/etc, so that they remain consistent with each other. (Though there are in fact some oddities, which might be a good subject for a future entry.)

Your design is better -- the warning should have gone into the scanner, not into the portion of the parser which turns numeric literal tokens into expression nodes in the abstract syntax tree.
July 29, 2004 1:21 PM
 

Steven Bone said:

Eric - talk about response times... 11:43 AM Nicholas Allen reports a possible bug. 12:01 PM Eric Lippert finds the internal source code, determines the problem, adds a warning in the code, and writes a response.
July 29, 2004 1:24 PM
 

Eric Lippert said:

Indeed.

I'm not actually going to FIX it, mind you. I don't own that code anymore. Next time I see the owner, I'll mention it to him, but this is a pretty low-priority fix.
July 29, 2004 1:27 PM
 

Fabulous Adventures In Coding said:

February 25, 2005 5:29 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication


© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker