有没有一种方法可以避免在括号内发表评论? [英] Is there a way to escape a closing bracket for comments?

查看:74
本文介绍了有没有一种方法可以避免在括号内发表评论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Delphi代码添加注释以进行文档编制。我要记录的一件事是JSON结构,因此我试图将示例注释的JSON数据放入代码中。但是,JSON广泛使用了波浪形括号 {} ,该括号也恰好也用于Delphi中的注释。

I'm adding comments to my Delphi code for documentation. One of the things I'm trying to document is the JSON structure, so I'm trying to put sample JSON data commented in the code. However, JSON widely uses the squiggly brackets { }, which coincidentally is also used for comments in Delphi.

因为为此,我想不出一种添加这些评论的方法。通常对于文档注释块,我使用 {} ,例如:

Because of this, I can't figure out a way to add these comments. Usually for documentation comment blocks I use { }, for example:

{
  This unit does this and that.

  Use TSomeComponent to do bla bla
}

当我尝试记录JSON数据时,右括号} 结束注释-即使该行以单行注释 // ,所以即使这样也不起作用:

When I try to document JSON data, the closing bracket } ends the comment - even if the line is prefixed with the single-line comment //, so even this doesn't work:

{
  This is how the JSON structure looks:

//    {
//      "some_string": "value",
//      "some_object": {
//        "something": 123
//      }, //<-- Compiler detects this comma
//      "something_else": "some other string"
//    }
}

只要有一个右括号} ,该注释行就变得无注释,甚至如果以两个斜杠为前缀。因此,Delphi在此括号之后选择了逗号。因此,最后,我无法找出在文档中记录JSON示例的任何方式。

As soon as there's a closing bracket }, the commented line becomes uncommented, even if it's prefixed with the two slashes. So Delphi picks up the comma after this bracket. So in the end, I cannot figure out any way in which I can document JSON samples inside my code.

我什至尝试使用(* * )在JSON块周围,但仍然没有运气。

I even tried using (* *) around the JSON block but still no luck.

有什么解决方法吗?还是我坚持使用它?

Is there any way around this or am I stuck with it?

推荐答案

我在输入此问题时发现了解决方案,所以现在我在回答它Q / A样式...

I discovered the solution while I was typing this question, so now I'm answering it Q/A style...

当注释块首先如上述问题中的代码中那样开始时,它以方括号 {开始。因此,即使该结束括号在另一行注释的代码中,编译器也拼命寻找一个结束括号。但是,如果此注释块中的每一行都以两个斜杠 // 开头,而不是一个开括号 {,则编译器将不会寻找右括号} 。因此,而不是上面的问题中的代码,这是应该这样写的:

When the comment block first starts as in the code in the question above, it starts with an opening bracket {. So, the compiler is desperately looking to find a closing bracket, even if that closing bracket is in another commented line of code. But, if every line in this comment block begins with two slashes // instead of an opening bracket {, then the compiler will not be looking for the closing bracket }. So, instead of the code in the question above, this is how it should be written:

//  This is how the JSON structure looks:
//  
//    {
//      "some_string": "value",
//      "some_object": {
//        "something": 123
//      },
//      "something_else": "some other string"
//    }

只要您不以尖括号开头注释块,那么编译器在找到右括号时就不会尝试结束注释块。

So as long as you don't start the comment block with an opening bracket, then the compiler won't try to end the comment block when it finds a closing bracket.

此外,使用(* *)应该可以正常工作,只要您在使用后不使用它已使用 {。用(* *)包围整个块,编译器将忽略任何 {} ,例如:

Also, using (* *) should work fine, as long as you don't use it after you've already used {. Enclose the entire block with (* and *) and the compiler will ignore any { or } inside, like so:

(*
  This is how the JSON structure looks:

    {
      "some_string": "value",
      "some_object": {
        "something": 123
      },
      "something_else": "some other string"
    }
*)

基本上,从您开始发表评论的那一刻起,无论是 // {(* ,它将忽略任何其他类型的注释的任何进一步打开-它只会查找其注释类型的结尾。因此 // 将查找行的末尾, {将查找} (* 将查找 *)

Essentially, from the moment you begin one comment, whether it be //, {, or (*, it will ignore any further opening of any other type of comment - it will only be looking for the end of its own comment type. So // will look for the end of the line, { will look for }, and (* will look for *).

这篇关于有没有一种方法可以避免在括号内发表评论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆