Content deleted Content added
Stevebroshar (talk | contribs) →Examples: Merge swift into curly brace section |
Stevebroshar (talk | contribs) →Examples: Merge perl into scripting section |
||
Line 343:
Syntax for comments varies by programming language. There are common patterns used by multiple languages while also a wide range of syntax among the languages in general. To limit the length of this section, some examples are grouped by languages with the same or very similar syntax. Others are for particular languages that have less common syntax.
===
Many of the [[curly brace language]]s such as C, C++ and their many derivatives delimit a line comment with {{code|//}} and a block comment with {{code|/*}} and {{code|*/}}. Originally, C lacked the line comment, but it was added in [[C99]]. Notable languages include: C, C++, [[C# (programming language)|C#]], [[Java (programming language)|Java]], [[Javascript]] and [[Swift (programming language)|Swift]]. For example:
Line 367 ⟶ 365:
</syntaxhighlight>
A pattern in many [[scripting language]]s is to delimit a line comment with <code>#</code> and provide no support for a block comment. Notable languages include: [[Bash (Unix shell)|Bash]], [[Perl]], [[Python (programming language)|Python]] and [[R (programming language)|R]].
An example in R:
Line 378 ⟶ 376:
</syntaxhighlight>
Instead of a regular block commenting construct, Perl uses [[literate programming]] [[Plain Old Documentation|plain old documentation (POD)]] markup.<ref>
{{cite web
| title = perlpod – the Plain Old Documentation format
| url = http://perldoc.perl.org/perlpod.html|access-date=2011-09-12
}}
</ref> For example:<ref>
{{cite web
| title = Pod::ParseUtils – helpers for POD parsing and conversion
| url = http://search.cpan.org/~bradapp/PodParser-1.20/lib/Pod/ParseUtils.pm|access-date=2011-09-12
}}</ref>
<syntaxhighlight lang="perl">
=item Pod::List-E<gt>new()
Create a new list object. Properties may be specified through a hash
reference like this:
my $list = Pod::List->new({ -start => $., -indent => 4 });
=cut
sub new {
...
}
</syntaxhighlight>
====Raku====
[[Raku (programming language)|Raku]] (previously called Perl 6) uses the same line comments and POD Documentation comments as regular [[Perl]] (see Perl section above), but adds a configurable block comment type: "multi-line / embedded comments".<ref name=perl6>
{{cite web
| title = Perl 6 Documentation – Syntax (Comments)
| url = https://docs.perl6.org/language/syntax#Comments|access-date=2017-04-06
}}
</ref>
These start with a hash character, followed by a backtick, and then some opening bracketing character, and end with the matching closing bracketing character.<ref name=perl6 /> The content can not only span multiple lines, but can also be embedded inline.
<syntaxhighlight lang="perl6">
#`{{ "commenting out" this version
toggle-case(Str:D $s)
Toggles the case of each character in a string:
my Str $toggled-string = toggle-case("mY NAME IS mICHAEL!");
}}
sub toggle-case(Str:D $s) #`( this version of parens is used now ){
...
}
</syntaxhighlight>
====Block in Python====
Although Python does not provide for block comments<ref name=triple>{{cite web |url=https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |title=Python 3 Basic Syntax |access-date=25 February 2019 |quote=Triple quotes are treated as regular strings with the exception that they can span multiple lines. By regular strings I mean that if they are not assigned to a variable they will be immediately garbage collected as soon as that code executes. hence are not ignored by the interpreter in the same way that #a comment is. |archive-date=19 August 2021 |archive-url=https://web.archive.org/web/20210819164828/https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |url-status=dead }}</ref> a bare [[string literal]] represented by a triple-quoted string is often used for this purpose.<ref>[https://twitter.com/gvanrossum/status/112670605505077248 "Python tip: You can use multi-line strings as multi-line comments"], 11 September 2011, Guido van Rossum</ref><ref name=triple/> In the examples below, the triple double-quoted strings act like comments, but are also treated as [[docstring]]s:
Line 666 ⟶ 715:
if (row + columnDifference = testRow) or
.......
</syntaxhighlight>
|