Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is semicolon really needed after declarations in x++?

As said in the book Microsoft Dynamics AX 2009 Programming: Getting Started it´s needed to put semicolons after declarations in x++:

The extra semicolon after the variable declaration is mandatory as long as the first line of code is not a keyword. The semicolon tells the compiler that variable declarations have come to an end. You cannot declare new variables after this semicolon.

(copied directly from the book, unchanged, if needed I'll remove it)

However, when I remove the semicolon and run the job, there's absolutely no error or problem:

static void Job1(Args _args)
{
    str string1 = "STACKOVERFLOW";
    ;
    print string1;
    pause;
}

works just as

static void Job2(Args _args)
{
     str string1 = "STACKOVERFLOW";

     print string1;
     pause;
}

Is it really needed? Should I get used to using it?

like image 427
Marcelo Avatar asked Dec 29 '09 18:12

Marcelo


2 Answers

It's explained rather elegantly here.

A key quote [emphasis mine]:

"The reason you need that extra semicolon is because the compiler can’t always see where the variable declarations end. If you don’t help a little, it will make a guess. And it’s not very good at guessing."

While the compiler is analyzing the code it checks if the first word on a line matches the name of a type (AOT object). If it’s a type name the compiler treats the line as a variable declaration. In this case a variable name should be next.

like image 143
Drew Dormann Avatar answered Oct 09 '22 00:10

Drew Dormann


With the release of AX 2012 there is no need to put the additional semicolon after variable declaration.

http://msdn.microsoft.com/en-us/library/aa636895.aspx

like image 28
Deepak Kalra Avatar answered Oct 08 '22 22:10

Deepak Kalra