Compartilhar via


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?

Comments

  • Anonymous
    July 29, 2004
    The comment has been removed
  • Anonymous
    July 29, 2004
    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?
  • Anonymous
    July 29, 2004
    Negative indices not allowed?
  • Anonymous
    July 29, 2004
    Why not?
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    Because "-1.23" is a string and -1.23 is a float? Since I'm not a jscript kinda person I'm not sure.
  • Anonymous
    July 29, 2004
    Larry: But in the example above, 1.23 works, so it can't be a float vs string issue.

  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    The comment has been removed
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    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 :)
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    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
  • Anonymous
    July 29, 2004
    JScript .NET (jsc 7.10.3052 to be exact)
  • Anonymous
    July 29, 2004
    The comment has been removed
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    The comment has been removed
  • Anonymous
    July 29, 2004
    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.
  • Anonymous
    July 29, 2004
    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.