Comments: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 329: Line 329:
Nothing following the end. will be evaluated by Toka.
Nothing following the end. will be evaluated by Toka.


=={{heaer|Ruby}}==
=={{header|Ruby}}==


x = "code" # I am a comment
x = "code" # I am a comment

Revision as of 22:47, 28 September 2007

Task
Comments
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate all ways to include text in a language source file which is completely ignored by the compiler or interpreter.

ActionScript

See Java

Ada

-- All Ada comments begin with "--" and extend to the end of the line


BASIC

100 REM Standard BASIC comments begin with "REM" (remark) and extend to the end of the line

Befunge

Like Brainfuck, all characters and whitespace which are not commands are ignored. Most punctuation, digits, and 'g' and 'p' are commands.

Also, since the code/data-space is two-dimensional, comments can be placed anywhere that will be untouched by the instruction pointer and data access commands.

& read a number 2+ add two .@ display result and exit
  ^- inline comments -^     <-^- other comments

Brainf***

This is a comment

Most ASCII characters may be used for comments; only the eight characters "+-<>[],." are Brainf*** commands. Extra care much be used when using punctuation, particularly the comma or period. These are I/O operators and are actual commands rather than comments, and are instead compiled into the program if used and may have to be "debugged" and removed if you forget this issue.

C

/* This is a comment. */
/* So is this
 * multiline comment.
 */

The comment starts at the /*, and ends at the */. A comment may be used between any tokens. It cannot be used inside tokens, that is, given the code

struct charisma {};
void f(char/* comment */isma) {}

the function takes an argument of type char, named isma, not an unnamed argument of type charisma.

Comments cannot be nested; that is, if you write

/* some comment /* trying to nest some other comment */ inside */

the comment ends at the first */, and inside */ is again interpreted as source code (almost certainly causing a compile error). Some compilers have the option to allow nested comments, but this is not a standard feature.

Conditional compilation also can be used to make the compiler ignore some text:

#if 0
While technically not a comment, this is also ignored by the compiler
#endif

The trick is that 0 is always false, therefore the text between #if 0 and #endif is never compiled.

While this should never be used for actual comments, it's an easy way to comment out some code, especially because it doesn't interfere with normal (documentation) comments. Note that the problem mentioned above cannot occur if there's valid code between the #if 0 and #endif.

Conditional compile "comments" can be nested:

#ifdef UNDEFINED
This is not compiled.
#if 0
Nor is this.
#endif
And this still is not compiled.
#endif

Standard: C99

// C++ single-line comments were adopted in the C99 standard.

C++

See also C

Single line C++-style comments

// This is a comment

C++-style comments start with // and reach up to, but not including, the end of line (more exactly, up to the next unescaped newline). While formally, C++-style comments cannot be nested either, in practice they can:

// This is a valid comment // with a "nested" comment

That's because starting with the first // everything in the line is ignored, including the second //. The fact that the newline is not part of the comment is important for multi-line macro defnitions. It means that in the code

#define FOO \
  (macro text) // comment
  (no more macro text)

the line (no more macro text) is not part of the macro definition. Also escaping the line break at the end of the comment with '\' doesn't help, because that would make the third line part of the comment instead. Comments inside macros therefore have to be C-style.

Clean

Clean comments are similar to C++.

Start = /* This is a multi-
           line comment     */ 17 // This is a single-line comment 

In contrast to C++ comments can be nested.

Start = /* This is a comment /* Nested comment */ still a comment */ 17

Common Lisp

; This is a single-line comment.
(defun foo ()
  "This function has a documentation string."
  (do-something))

Documentation strings are optional and may span multiple lines as string literals do. In addition to defun, many other constructs may also include documentation strings. Among these are defmacro, defmethod, defstruct, defclass, defvar, defparameter, and defconstant.

Delphi

See Object Pascal

E

# This is a regular comment.
? "This is an Updoc comment, which
> is an executable example or test case.".split(" ")
# value: ["This", "is", "an", "Updoc", "comment,", "which
#        is", "an", "executable", "example", "or", "test", "case."]

Erlang

% Erlang comments begin with "%" and extend to the end of the line.

Forth

Standard Forth includes a number of ways to add comment text. As with everything in Forth, comment characters are actually words that control the compiler.

\ The backslash skips everything else on the line
( The left paren skips everything up to the next right paren on the same line)

Traditionally, the paren comments are used for "stack effect" notation:

: myword ( a b -- c )  ...

This comment means "myword takes two cells on the stack and leaves one". Sometimes, stack effect comment names give clues about the word's function:

: add'em ( a b -- a+b )   + ;
: strlen ( addr -- len )   count nip ;

Some Forth systems implement other commenting words, such as these words from Win32Forth:

\s skips all remaining text in the file
(( skips until the next double-paren, 
   stretching across multiple lines ))
comment:
   Ignore all text in this section
comment;
doc
   Another comment block
enddoc
/* C-style comment */
(* Pascal-style comment *)

Fortran

Compiler: ANSI F77 or compatible (like g77 -strict)

The first six columns in Fortran are traditionally reserved for labels and certain special characters. In particular a letter in the first column indicates a comment. This can be any character but is usually a "C" by convention:

C     This would be some kind of comment
C     Usually one would avoid columns 2-6 even in a comment.

Some Fortran compilers have the extension that comments starting with D are treated as non-comments if a special debugging flag is given at the compiler invocation. For example:

C     If compiled in debugging mode, print the current value of I
D     PRINT *, I

Haskell

 i code = True -- I am a comment.
 
 {- I am also
    a comment.
    let u x = x x (this code not compiled)
    Are you? -}

IDL

The comment character in IDL is the semicolon - everything starting with it and to the end of the line is a comment. Like this:

; The following computes the factorial of a number "n"
fact = product(indgen( n )+1) ; where n should be an integer

Java

Java has two ways to enter comments.

/* This is a comment */

This C-style comment starts with /* and ends with */. The two delimiters may be on the same or separate lines. This style comment may be used anywhere white space is permitted.

// This is a comment

This C++-style comment starts with // and extends to the end of line.

JavaScript

See Java

LaTeX

In LaTeX, comments look like this:

% This is a comment

LaTeX comments start with % and continue up to and including the line break. The fact that the line break itself is commented out as well makes it useful for adding line breaks in the source code of complex macros without LaTeX interpreting them (which may cause extra space or even a paragraph break in the resulting typeset text). For example, the following results in the one word "understandable":

\newcommand{\firstpart}{understand}
\newcommand{\secondpart}{able}
\newcommand{\complete}{%
\firstpart%
\secondpart}

\complete

Without the percent sign after \firstpart, it would have been the two words "understand able".

Lua

 -- A single line comment
 --[[A multi-line 
     comment --]]

MAXScript

-- Two dashes precede a single line comment
/* This is a
   multi-line comment */

Objective-C

See C

Object Pascal

See Pascal
{ Starting in Turbo Pascal,
  curly quotes could also be used for comments. }

Pascal

(* This is a comment.
   It may exend across multiple lines. *)

Perl

Interpreter: Perl 5.x

Single line comment

# this is commented

These may also be at the end of a line

my $var = 1; # this is the comment part

Multi-line comment

=pod
Here are my comments
this is multi-line
=cut

Pop11

Pop11 has two kinds of comments: endline and C-like. Endline comment begins with tree consequitive semicolons and ends at the end of line:

;;; This is a comment

C-like comments may be multiline:

/* First line
   Second line */

C-like comments (unlike C) may be nested:

 /* This is a comment /* containing nested comment */ */

One can also use conditional compilation to comment out sections of code

#_IF false
some code 
#_ENDIF

however, commented out part must consist of valid Pop11 tokens. In particular, C-like comments must balance and strings must be terminated. The following is an error:

#_IF false
This w'ont work
#_ENDIF

because apostrophe starts an unterminated string.

Python

Python uses the "#" symbol to mark it's comments. After placing a "#", everything in that line will be ignored.

# This is a comment
foo = 5 # You can also put comments in front of commands

Tcl

Tcl follows the usual scripting language rules: a comment starts at a "#" symbol, which can be placed after a command if that is terminated by a semicolon:

# comment on a line by itself. The next is a command by itself:
set var1 $value1
set var2 $value2 ; # comment that follows a line of code

Toka

There are two ways to add comments in Toka. For full lines, or at the end of a line, the shebang is normally used:

#! Everything on this line (after the shebang to the left) will be ignored.

The shebang comments can not be used inside of functions.

In addition, Toka also accepts parenthetical comments. These are enclosed in parenthesis, and are often used for stack comments or comments inside functions.

[ ( a b -- c ) 
  ... ] is myword

In addition, parenthetical comments can span multiple lines.

( This is a
  simple, multi-line
  comment )

Since comments are provided by actual functions, the comment function must be whitespace delimited, just as with all other functions in Toka.

A final way to include text in a file is to mark a false ending with end.

... code ....
end.
Nothing following the end. will be evaluated by Toka.

Ruby

 x = "code" # I am a comment
 
 =begin hello
 I am comment
 =end puts "code"

Smalltalk

"Comments traditionally are in double quotes."
"* This form is also supported in many Smalltalk dialects.
   Comments are saved as metadata along with the source to a method.
   A comment just after a method signature is often given to explain the
   usage of the method. The class browser may display such comments
   specially. *"