x++ 中的声明后真的需要分号吗?

发布于 2024-08-16 10:59:17 字数 567 浏览 5 评论 0原文

正如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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂ガ小子 2024-08-23 10:59:17

此处对此进行了相当优雅的解释。

关键引用[强调我的]:

“你需要额外的分号的原因是因为编译器不能总是看到变量声明在哪里结束。如果你不帮忙一点,它就会做出猜测.而且它不太擅长猜测。”

当编译器分析代码时,它检查行上的第一个单词是否与类型(AOT 对象)的名称匹配。如果它是类型名称,编译器会将该行视为变量声明。在这种情况下,接下来应该是变量名称。

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.

Oo萌小芽oO 2024-08-23 10:59:17

随着 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

爱她像谁 2024-08-23 10:59:17

如果代码正文不以关键字开头,则仅需要分号。在您的示例中,您的代码以 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?

客…行舟 2024-08-23 10:59:17

当声明(如果有)之后的下一个单词不是编译器识别的关键字时,您真的不需要可爱的分号(您不会收到编译错误)像类型(EDT、表、类...)

例如:

void method1()
{
    CustTable    custTable;

    custTable = CustTable::find("cust");
}

错误!,因为编译器无法分隔 X++ 代码开头的类声明块。当编译器读取第二行时,它不知道 custTable 是新变量还是 X++ 代码的一部分。因此,您需要额外的分号来告诉编译器声明的结尾在哪里(实际上,X++ 代码的开头在哪里)。

void method1()
{
    CustTable    custTable;

    if (custTable)
    {
        // stuff happens
    }        
}

有效!因为编译器知道您不能声明 if 类型的变量(显然,这是一个保留关键字),因此很明显这是 X++ 代码的开始,并且您不能在此行之后声明变量。

即使没有变量声明,这也是如此:

CustTable method1()
{
    custTable = CustTable::find("cust"); // custTable may exists in the context
    return custTable;
}

ERROR! custTable 可能是一个声明,或者像该示例那样的 X++ 代码。

CustTable method1()
{
    return CustTable::find("cust");
}

WORKS! 因为 return 不能是声明。

额外:

void method1()
{
    info("This should work, ya?");
}

这应该可以工作(因为 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:

void method1()
{
    CustTable    custTable;

    custTable = CustTable::find("cust");
}

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).

void method1()
{
    CustTable    custTable;

    if (custTable)
    {
        // stuff happens
    }        
}

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:

CustTable method1()
{
    custTable = CustTable::find("cust"); // custTable may exists in the context
    return custTable;
}

ERROR! custTable may be a decaration, or X++ code like that example.

CustTable method1()
{
    return CustTable::find("cust");
}

WORKS! as return can't be a declaration.

EXTRA:

void method1()
{
    info("This should work, ya?");
}

This should work (as info is not a type), isn't it? ... but it doesn't! Why? Because info is an special kernel method that will be replaced to its full name: Global::info(), first token will be Global after the precompiler replacement, and Global is a class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文