x++ 中的声明后真的需要分号吗?
正如Microsoft Dynamics AX 2009 编程:入门一书中所述,需要在 x++ 中的声明后添加分号:
变量后面多余的分号 声明是强制性的,只要 第一行代码不是 关键词。分号告诉 编译器认为变量声明 已经结束了。你不能 之后声明新变量 分号。
(直接从书中复制,未更改,如果需要我将删除它)
但是,当我删除分号并运行作业时,绝对没有错误或问题:
static void Job1(Args _args)
{
str string1 = "STACKOVERFLOW";
;
print string1;
pause;
}
工作就像
static void Job2(Args _args)
{
str string1 = "STACKOVERFLOW";
print string1;
pause;
}
真的需要吗?我应该习惯使用它吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此处对此进行了相当优雅的解释。
关键引用[强调我的]:
It's explained rather elegantly here.
A key quote [emphasis mine]:
随着 AX 2012 的发布,无需在变量声明后添加额外的分号。
http://msdn.microsoft.com/en-us/library/aa636895.aspx
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
如果代码正文不以关键字开头,则仅需要分号。在您的示例中,您的代码以
print
开头,这是一个内置关键字。如果您尝试使用以下代码开始编码:string1+=".COM";
,您将收到错误。Dynamics AX 2009 是需要额外分号的最后一个 AX 版本。 AX 6.0 应该修复此问题: mfp 的两分钱:这个分号是怎么回事?
You only need the semicolon if the body of your code doesn't start with a keyword. In your example, your code starts with
print
, which is a built in keyword. If you had tried to start you code with:string1+=".COM";
you would receive an error.Dynamics AX 2009 is the last version of AX that will require the extra semicolon. AX 6.0 should fix this: mfp's two cents: What's up with this semicolon?
当声明(如果有)之后的下一个单词不是编译器识别的关键字时,您真的不需要可爱的分号(您不会收到编译错误)像类型(EDT、表、类...)
例如:
错误!,因为编译器无法分隔 X++ 代码开头的类声明块。当编译器读取第二行时,它不知道 custTable 是新变量还是 X++ 代码的一部分。因此,您需要额外的分号来告诉编译器声明的结尾在哪里(实际上,X++ 代码的开头在哪里)。
有效!因为编译器知道您不能声明
if
类型的变量(显然,这是一个保留关键字),因此很明显这是 X++ 代码的开始,并且您不能在此行之后声明变量。即使没有变量声明,这也是如此:
ERROR!
custTable
可能是一个声明,或者像该示例那样的 X++ 代码。WORKS! 因为
return
不能是声明。额外:
这应该可以工作(因为
info
不是类型),不是吗? ...但事实并非如此!为什么?因为info
是一个特殊的内核方法,它将被替换为它的全名:Global::info()
,第一个标记将是Global
之后预编译器替换,Global
是一个类。You really don't need the lovely semicolon (you don't get a compilation error) when the next word after declarations (if any) is not some keyword recognized by compilator like a type (an EDT, table, class, ...)
For example:
ERROR! as compiler can't separate the class declaration block of the start of the X++ code. When compilator reads the second line it doesn't know if custTable is a new variable or is part of the X++ code. So that, you need the extra semicolon to say the compiler where is the end of declarations (really, where is the start of X++ code).
WORKS! as compiler knows that you can't declare a variable of type
if
(it's a reserved keyword, obviously) so it's clear that this is the beginning of X++ code and you can't declare variables after this line.This works that way even if there is no variable declarations:
ERROR!
custTable
may be a decaration, or X++ code like that example.WORKS! as
return
can't be a declaration.EXTRA:
This should work (as
info
is not a type), isn't it? ... but it doesn't! Why? Becauseinfo
is an special kernel method that will be replaced to its full name:Global::info()
, first token will beGlobal
after the precompiler replacement, andGlobal
is a class.