Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between {} and //?

Are the {} and // both for adding comments depending on size?

Sorry for the silly question, but when ever I try and google "{} // Delphi" I get links to Delphi.com and Wikipidia for random information.

like image 902
Aidan Quinn Avatar asked Feb 18 '14 14:02

Aidan Quinn


3 Answers

There are three ways to add comment to a delphi source file:

{ This can be a single line comment }

{   but it can also span multiple lines
}

// Single line comment

The single line comment ends at the end of the line. The other comments have their own end.

(* This also can be a single comment *)

(* And it also can span multiple lines
*)

{ Can be used to comment out code containing a (* comment *)
  // Or one of those 
}
(* Can be used to coment out code containing a { comment } 
   // Or one of those 
 *)

// And this can contain the (* Single *) version of the { other } comments.

There is no real difference. But some people reserve one comment style to (temporary) comment out code. Because you can't nest the same type but you can nest different types.

Trivia, the (* *) comment is included to support (real) old keyboards that had no { and }. You can also use (. .) for [].

like image 128
Toon Krijthe Avatar answered Oct 05 '22 01:10

Toon Krijthe


The first place to look at is the documentation.

DELPHI: Fundamental Syntactic Elements - Comments and Compiler Directives


Comments and Compiler Directives

Comments are ignored by the compiler, except when they function as separators (delimiting adjacent tokens) or compiler directives.

There are several ways to construct comments:

{ Text between left and right braces is a comment. }
(* Text between left-parenthesis-plus-asterisk and an 
 asterisk-plus-right-parenthesis is also a comment *)
// Text between double-slash and end of line is a comment.

Comments that are alike cannot be nested. For instance, (*{}*) will. This latter form is useful for commenting out sections of code that also contain comments.
Here are some recommendations about how and when to use the three types of comment characters:

  • Use the double-slash (//) for commenting out temporary changes made during development. You can use the Code Editor's convenient CTRL+/ (slash) mechanism to quickly insert the double-slash comment character while you are working.
  • Use the parenthesis-star (*...*) both for development comments and for commenting out a block of code that contains other comments. This comment character permits multiple lines of source, including other types of comments, to be removed from consideration by the compiler.
  • Use the braces ({}) for in-source documentation that you intend to remain with the code.

A comment that contains a dollar sign ($) immediately after the opening { or (* is a compiler directive. For example,

{$WARNINGS OFF}

tells the compiler not to generate warning messages.


Examples

// single line comment

WriteLn( { inline comment } 'hello' );

{ multi
  line
  comment }
like image 27
Sir Rufo Avatar answered Oct 05 '22 01:10

Sir Rufo


correct me if I'm wrong, but I think this might help you along:

//This is a single line comment.

{
Multiple line
comment.
}

(*
This too is a
multiple line comment.
*)
like image 39
LievenV Avatar answered Oct 05 '22 00:10

LievenV