<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx</link><description>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.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200921</link><pubDate>Thu, 29 Jul 2004 18:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200921</guid><dc:creator>Michael Ruck</dc:creator><description>I guess it attempts to create a named field &amp;quot;-1&amp;quot; and set it to 0. However since this is an invalid field identifier, it fails.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200932</link><pubDate>Thu, 29 Jul 2004 18:18:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200932</guid><dc:creator>Eric Lippert</dc:creator><description>Bzzt.  Try again.  As you can see above, &amp;quot;-1.23&amp;quot; is a valid field identifier.  Why should -1 be illegal when &amp;quot;-1.23&amp;quot; is legal?</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200935</link><pubDate>Thu, 29 Jul 2004 18:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200935</guid><dc:creator>yipyip</dc:creator><description>Negative indices not allowed?</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200936</link><pubDate>Thu, 29 Jul 2004 18:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200936</guid><dc:creator>Eric Lippert</dc:creator><description>Why not?</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200938</link><pubDate>Thu, 29 Jul 2004 18:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200938</guid><dc:creator>Anderson Imes</dc:creator><description>It's seems it's evaluating the minus sign as an operator instead of an indication of &amp;quot;below zero&amp;quot;.  Thus, it's looking for an identifier, string or number before the minus to get the left hand side of the subtraction operation.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200939</link><pubDate>Thu, 29 Jul 2004 18:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200939</guid><dc:creator>Larry Osterman</dc:creator><description>Because &amp;quot;-1.23&amp;quot; is a string and -1.23 is a float?  Since I'm not a jscript kinda person I'm not sure.  </description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200940</link><pubDate>Thu, 29 Jul 2004 18:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200940</guid><dc:creator>Eric Lippert</dc:creator><description>Larry: But in the example above, 1.23 works, so it can't be a float vs string issue.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200943</link><pubDate>Thu, 29 Jul 2004 18:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200943</guid><dc:creator>G Man</dc:creator><description>The 1.23 works because it is positive. The &amp;quot;-1.23&amp;quot; works because it is in quotes. I think the people above had it right. I'm just summing up what they said.&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200948</link><pubDate>Thu, 29 Jul 2004 18:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200948</guid><dc:creator>Neil Mitchell</dc:creator><description>because &amp;quot;-1.23&amp;quot; is a string, and -1 isn't.&lt;br&gt;&lt;br&gt;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.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200952</link><pubDate>Thu, 29 Jul 2004 18:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200952</guid><dc:creator>Eric Lippert</dc:creator><description>Anderson got it.  &lt;br&gt;&lt;br&gt;Holy cow, twenty-six minutes from question to correct answer.  Don't you people have jobs?  :-)&lt;br&gt;&lt;br&gt;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.&lt;br&gt;&lt;br&gt;There is a similar gotcha in VBScript:&lt;br&gt;&lt;br&gt;print typename(-32767-1) ' Integer&lt;br&gt;print typename(-32768)   ' Long&lt;br&gt;&lt;br&gt;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.&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200963</link><pubDate>Thu, 29 Jul 2004 18:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200963</guid><dc:creator>Larry Osterman</dc:creator><description>Oh, and Eric: I'm trolling blogs from home right now waiting to take daniel to his dress rehearsal :)&lt;br&gt;&lt;br&gt;That's why I have time for this.&lt;br&gt;&lt;br&gt;You should look at the time stamps on my &amp;quot;what's wrong with this code&amp;quot; posts - they're scary :)&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200964</link><pubDate>Thu, 29 Jul 2004 18:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200964</guid><dc:creator>Nicholas Allen</dc:creator><description>Since we're talking about the quirks of numbers in object initializers...&lt;br&gt;&lt;br&gt;Why doesn't&lt;br&gt;&lt;br&gt;var x = { 08 : 0 };&lt;br&gt;&lt;br&gt;whine about being a malformed octal literal?&lt;br&gt;&lt;br&gt;var y = 08;&lt;br&gt;&lt;br&gt;does.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200969</link><pubDate>Thu, 29 Jul 2004 18:47:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200969</guid><dc:creator>Eric Lippert</dc:creator><description>I don't get any error for either of those.  Or are you talking about JScript .NET?&lt;br&gt;&lt;br&gt;I talked a bit about malformed octal here:&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://blogs.msdn.com/ericlippert/archive/2003/10/23/53278.aspx"&gt;http://blogs.msdn.com/ericlippert/archive/2003/10/23/53278.aspx&lt;/a&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200972</link><pubDate>Thu, 29 Jul 2004 18:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200972</guid><dc:creator>Nicholas Allen</dc:creator><description>JScript .NET (jsc 7.10.3052 to be exact)</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200984</link><pubDate>Thu, 29 Jul 2004 19:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200984</guid><dc:creator>Eric Lippert</dc:creator><description>Ah, how interesting.  That's a bug.  &lt;br&gt;&lt;br&gt;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.&lt;br&gt;&lt;br&gt;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.&lt;br&gt;&lt;br&gt;I'm not sure why the compiler author made that choice -- I didn't write that part of the compiler.&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#200993</link><pubDate>Thu, 29 Jul 2004 19:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:200993</guid><dc:creator>James Mastros</dc:creator><description>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.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#201014</link><pubDate>Thu, 29 Jul 2004 19:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201014</guid><dc:creator>Nicholas Allen</dc:creator><description>Interesting.  That is kind of weird to use a runtime function to do the conversion.&lt;br&gt;&lt;br&gt;We check for bad octals in the scanner so the warning has already gone out before anyone gets their hands on the token.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#201038</link><pubDate>Thu, 29 Jul 2004 20:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201038</guid><dc:creator>Eric Lippert</dc:creator><description>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.)&lt;br&gt;&lt;br&gt;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.</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#201043</link><pubDate>Thu, 29 Jul 2004 20:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201043</guid><dc:creator>Steven Bone</dc:creator><description>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.&lt;br&gt;</description></item><item><title>re: Pop Quiz</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#201046</link><pubDate>Thu, 29 Jul 2004 20:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:201046</guid><dc:creator>Eric Lippert</dc:creator><description>Indeed.&lt;br&gt;&lt;br&gt;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.</description></item><item><title>VBScript Quiz Answers, Part Two</title><link>http://blogs.msdn.com/ericlippert/archive/2004/07/29/pop-quiz.aspx#380662</link><pubDate>Sat, 26 Feb 2005 01:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:380662</guid><dc:creator>Fabulous Adventures In Coding</dc:creator><description /></item></channel></rss>