{"id":62,"date":"2010-08-28T15:17:02","date_gmt":"2010-08-28T14:17:02","guid":{"rendered":"http:\/\/81.86.147.124:7564\/blog\/?p=62"},"modified":"2010-08-28T15:22:27","modified_gmt":"2010-08-28T14:22:27","slug":"grammar-changes","status":"publish","type":"post","link":"https:\/\/bryants.eu\/blog\/2010\/08\/grammar-changes\/","title":{"rendered":"Grammar changes"},"content":{"rendered":"<p>I have just made some changes to the grammar, specifically relating to anonymous inner classes and array instantiations.<\/p>\n<p>The problem is caused by the fact that conditionals in if and while statements do not have to have parentheses around them. This causes conflicts between the class member list\/array initializer\u00a0and the block of the if\/while statement. Here are two examples:<\/p>\n<pre>class X {\r\n\u00a0\u00a0void foo() {\r\n\u00a0\u00a0 \u00a0if blah == new Runnable()\r\n\u00a0\u00a0 \u00a0{} \/\/ member list, or block for the if statement?\r\n\u00a0\u00a0 \u00a0{} \/\/ block for the if statement, or another block after it?\r\n\u00a0\u00a0}\r\n}<\/pre>\n<pre>class X {\r\n\u00a0\u00a0void foo() {\r\n\u00a0\u00a0 \u00a0if blah == new X[0]\r\n\u00a0\u00a0 \u00a0{} \/\/ array initializer for \"new X[0]\", or block for the if statement\r\n\u00a0\u00a0 \u00a0{} \/\/ block for the if statement, or another block after it?\r\n\u00a0\u00a0}\r\n}<\/pre>\n<p>Since there is no way to tell which is meant in either of these cases, it is necessary to change the grammar and avoid this ambiguity.<\/p>\n<p>For the first example, the syntax for anonymous inner classes has been changed to be slightly more readable, by adding a &#8220;class&#8221; keyword between the constructor parameters and the member list:<\/p>\n<pre>class X {\r\n\u00a0\u00a0void foo() {\r\n\u00a0\u00a0 \u00a0if blah == new Runnable() class\r\n\u00a0\u00a0 \u00a0{} \/\/ member list\r\n\u00a0\u00a0 \u00a0{} \/\/ block for the if statement\r\n\u00a0\u00a0}\r\n}<\/pre>\n<p>For the second example, the offending case has been disallowed. It is no longer possible to use an array initializer if the number of elements in the array is provided. Therefore the new syntax is:<\/p>\n<pre>class X {\r\n\u00a0\u00a0void foo() {\r\n\u00a0\u00a0 \u00a0if blah == new X[]\r\n\u00a0\u00a0 \u00a0{} \/\/ array initializer for \"new X[]\"\r\n\u00a0\u00a0 \u00a0{} \/\/ block for the if statement\r\n\u00a0\u00a0}\r\n}<\/pre>\n<p>This was a difficult decision, however I think it is better to disallow this relatively uncommon scenario in favour of removing the parentheses from if and while statements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have just made some changes to the grammar, specifically relating to anonymous inner classes and array instantiations. The problem is caused by the fact that conditionals in if and while statements do not have to have parentheses around them. This causes conflicts between the class member list\/array initializer\u00a0and the block of the if\/while statement. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":2,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bryants.eu\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}