Skip to content

Commit

Permalink
Rephrases and fixes some errors
Browse files Browse the repository at this point in the history
Including some former errors that had not been spotted.
  • Loading branch information
JJ committed Oct 4, 2018
1 parent 05911ce commit 5c78c26
Showing 1 changed file with 73 additions and 68 deletions.
141 changes: 73 additions & 68 deletions doc/Language/syntax.pod6
Expand Up @@ -221,7 +221,8 @@ of comment
say 'code again';
=end code
=head2 X<Identifiers|identifier,identifiers>
X<identifier>
=head2 Identifiers
Identifiers are grammatical building blocks that may be used to give a name
to entities/objects such as constants, variables (e.g. Scalars) and routines
Expand Down Expand Up @@ -249,7 +250,7 @@ characters with the Unicode General Category value I<Letter> (L), and the
underscore C<_>. Alphanumeric characters additionally include characters with
the Unicode General Category value I<Number, Decimal Digit> (Nd).
=begin code
=begin code :skip-test<bad identifiers>
# valid ordinary identifiers:
x
_snake_oil
Expand All @@ -260,7 +261,7 @@ piece_of_π
駱駝道 # "Rakuda-dō", Japanese for "Way of the camel"
=end code
=begin code
=begin code :skip-test<bad identifiers>
# invalid ordinary identifiers:
42 # identifier does not start with alphabetic character
with-numbers1234-5 # embedded hyphen not followed by alphabetic character
Expand All @@ -270,82 +271,84 @@ x² # superscript 2 is not alphanumeric (in the sense explain
=head3 Extended identifiers
It is often convenient to have names that contain characters that are not allowed
in ordinary identifiers. Use cases include situations where a set of entities shares
a common "short" name, but still needs for each of its elements to be identifiable
individually. For example, you might use a module whose short name is C<Dog>, while
its long name includes its naming authority and version:
It is often convenient to have names that contain characters that are not
allowed in ordinary identifiers. Use cases include situations where a set of
entities shares a common "short" name, but still needs for each of its elements
to be identifiable individually. For example, you might use a module whose short
name is C<Dog>, while its long name includes its naming authority and version:
=begin code
Dog:auth<Somebody>:ver<1.0> # long module names including author and version
=begin code :skip-test<identifiers only>
Dog:auth<Somebody>:ver<1.0> # long module names including author and version
Dog:auth<Somebody>:ver<2.0>
------------------------------------
use Dog:auth<Somebody>:ver<2.0>;
use Dog:auth<Somebody>:ver<2.0>; # Selection of second module causes its full name
# to be aliased to the short name for the rest of
# the lexical scope, allowing a declaration like
my Dog $spot .= new("woof"); # this.
# Selection of second module causes its full name to be aliased to the short
# name for the rest of # the lexical scope, allowing a declaration like
# this.
my Dog $spot .= new("woof");
=end code
Similarly, sets of operators work together in various syntactic categories with names
like C<prefix>, C<infix>, C<postfix>, etc. The long, official names of these operators
often contain characters that are excluded from ordinary identifiers:
Similarly, sets of operators work together in various syntactic categories with
names like C<prefix>, C<infix> and C<postfix>. The official names of these
operators often contain characters that are excluded from ordinary identifiers.
The long name is what constitutes the extended identifier, and includes this
syntactic category; the short name will be included in quotes in the definition:
=begin code
infix:<+> # the official name of the operator in $a + $b
infix:<*> # the official name of the operator in $a * $b
infix:«<=» # the official name of the operator in $a <= $b
=begin code :skip-test<identifiers only>
infix:<+> # the official name of the operator in $a + $b
infix:<*> # the official name of the operator in $a * $b
infix:«<=» # the official name of the operator in $a <= $b
=end code
For all such uses, you can append one or more "colon pairs" to an ordinary identifier
to create a so-called I<extended identifier>. When appended to an identifier (that is,
in postfix position), the colon pair generates unique variants of that identifier.
For all such uses, you can append one or more colon-separated string to an
ordinary identifier to create a so-called I<extended identifier>. When appended
to an identifier (that is, in postfix position), this colon-separated string
generates unique variants of that identifier.
The generic colon pair syntax acceptable in identifiers is C<:key<value>>, wherein
C<key> I<or> C<value> is optional. A colon pair thus starts with a single colon C<:>,
followed by an ordinary identifier C<key> and/or a quoting bracketing construct
such as C«< >», C<« »> or C<[' ']> which quotes one or more arbitrary characters
C<value>:
These strings have the form C<:key<value>>, wherein C<key> I<or> C<value> are
optional; that is, after the colon that separates it from a regular identifier,
there will be a C<key> and/or a quoting bracketing construct such as C«< >», C<« »>
or C<[' ']> which quotes one or more arbitrary characters C<value>:
=begin code
=begin code :skip-test<Identifiers good and bad>
# exemplary valid extended identifiers:
postfix:<²> # the official long name of the operator in $x²
postfix:<²> # the official long name of the operator in $x²
WOW:That'sAwesome
WOW:That's<<🆒>>
party:sweet<16>
# exemplary invalid extended identifiers:
party:16<sweet> # 16 is not an ordinary identifier
party:16<sweet> # 16 is not an ordinary identifier
party:16sweet
party:!a # ...and neither is !a
party:$a # ...nor $a
party:!a # ...and neither is !a
party:$a # ...nor $a
=end code
N<Starting with Perl 6 language version 6.d, colon pairs with C<sym> as the
C<key> (e.g. C«:sym<foo>») are reserved for possible future use.>
In an extended identifier, the colon pair is considered an integral part of the name,
so C<infix:<+>> and C<infix:<->> are two different operators. The bracketing
characters used, however, do not count as part of the name; only the quoted data
matters. So these are all the same name:
In an extended identifier, the postfix string is considered an integral part of
the name, so C<infix:<+>> and C<infix:<->> are two different operators. The
bracketing characters used, however, do not count as part of it; only the
quoted data matters. So these are all the same name:
=begin code
infix:<+>
infix:<<+>>
infix:«+»
infix:['+']
infix:('+')
=begin code :skip-test<identifiers only>
infix:<+>
infix:<<+>>
infix:«+»
infix:['+']
infix:('+')
=end code
Similarly, all of this works:
=begin code
my $foo:bar<baz> = 'quux';
say $foo:bar«baz»; # OUTPUT: «quux␤»
my $take-me:<home> = 'When the glory has no end';
say $take-me:['home']; # OUTPUT: «When the glory has no end␤»
my $foo:bar<2> = 5;
say $foo:bar(1+1); # OUTPUT: «5␤»
my $foo:bar<baz> = 'quux';
say $foo:bar«baz»; # OUTPUT: «quux␤»
my $take-me:<home> = 'When the glory has no end';
say $take-me:['home']; # OUTPUT: «When the [...]␤»
my $foo:bar<2> = 5;
say $foo:bar(1+1); # OUTPUT: «5␤»
=end code
Where an extended identifier comprises two or more colon pairs, their order
Expand All @@ -360,19 +363,19 @@ is generally significant:
An exception to this rule is I<module versioning>; so these identifiers
effectively name the same module:
=begin code
use ThatModule:auth<Somebody>:ver<2.7.18.28.18>
use ThatModule:ver<2.7.18.28.18>:auth<Somebody>
=begin code :skip-test<fake modules>
use ThatModule:auth<Somebody>:ver<2.7.18.28.18>
use ThatModule:ver<2.7.18.28.18>:auth<Somebody>
=end code
Furthermore, colon pairs in extended identifiers support
compile-time interpolation; this requires the use of
L<constants|/language/terms#Constants> for the interpolation values:
Furthermore, extended identifiers support compile-time interpolation; this
requires the use of L<constants|/language/terms#Constants> for the interpolation
values:
=begin code
constant $c = 42; # Constant binds to Int; $-sigil enables interpolation
my $a:foo<42> = "answer";
say $a:foo«$c»; # OUTPUT: «answer␤»
constant $c = 42; # Constant binds to Int; $-sigil enables interpolation
my $a:foo<42> = "answer";
say $a:foo«$c»; # OUTPUT: «answer␤»
=end code
Although quoting bracketing constructs are generally interchangeable
Expand All @@ -381,11 +384,11 @@ angle brackets C«< >» (which mimic single quote interpolation
characteristics) cannot be used for the interpolation of constant
names.
=begin code
constant $what = 'are';
my @we:<are>= <the champions>;
say @we:«$what»; # OUTPUT: «[the champions]␤»
say @we:<$what>; # Compilation error: Variable '@we:<$what>' is not declared
=begin code :skip-test<illustrates error>
constant $what = 'are';
my @we:<are>= <the champions>;
say @we:«$what»; # OUTPUT: «[the champions]␤»
say @we:<$what>; # Compilation error: Variable '@we:<$what>' is not declared
=end code
=head3 Compound identifiers
Expand Down Expand Up @@ -414,9 +417,11 @@ Separating identifiers with double colons causes the rightmost name to be
inserted into existing (see above example) I<or automatically created>
packages:
my $foo::bar = 1;
say OUR::.keys; # OUTPUT: «(foo)␤»
say foo.HOW # OUTPUT: «Perl6::Metamodel::PackageHOW.new␤»
=begin code
my $foo::bar = 1;
say OUR::.keys; # OUTPUT: «(foo)␤»
say OUR::foo.HOW # OUTPUT: «Perl6::Metamodel::PackageHOW.new␤»
=end code
The last lines shows how the C<foo> package was created automatically, as a
deposit for variables in that namespace.
Expand Down

0 comments on commit 5c78c26

Please sign in to comment.