Skip to content

Commit 6150f77

Browse files
Rewrite of Identifier-section
1 parent 9545ed4 commit 6150f77

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

doc/Language/syntax.pod6

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -268,52 +268,73 @@ is-prime? # Question mark is not alphanumeric
268268
x² # Superscript 2 has Unicode general category No
269269
=end code
270270
271-
In the C<> example above, the Unicode superscript numeral C<²> is not, and
272-
cannot be, a part of an ordinary identifier. Still, it may form part of a valid
273-
expression as an operator. The expression C<$x²>, for instance, produces the
274-
square of variable C<$x>. This should be somewhat surprising at this point,
275-
since operators are L<Subs|/type/Sub>, and, as mentioned above, Subs are named using
276-
identifiers. So how does the superscript numeral get accepted as (part of) an
277-
identifier? The key to this riddle lies in the concept of I<extended
271+
In the C<> example above, since the Unicode superscript numeral C<²> is not
272+
alphanumeric, it cannot be a part of an ordinary identifier. Still, it may form
273+
part of a valid expression as an operator. The expression C<$x²>, for instance,
274+
produces the square of variable C<$x>. This should be somewhat surprising at this
275+
point, since operators are L<Subs|/type/Sub>, and, as mentioned above, Subs are
276+
named using identifiers. So how does the superscript numeral get accepted as
277+
(part of) an identifier? The key to this riddle lies in the concept of I<extended
278278
identifiers>, which is elaborated upon in the next section.
279279
280280
=head3 Extended identifiers
281281
282-
It is often convenient to have names that contain arbitrary characters.
283-
Use cases include situations where a set of entities shares a common "short"
284-
name, but still needs for each of its elements to be identifiable individually.
285-
For example, you might use a module whose short name is C<ThatModule>, but the
286-
complete long name of a module includes its version, naming authority, and
287-
perhaps even its source language. Similarly, sets of operators work together in
288-
various syntactic categories with names like C<prefix>, C<infix>, C<postfix>,
289-
etc. The (long) names of these operators, however, often contain characters that
290-
are excluded from ordinary identifiers.
291-
292-
For all such uses, you can append one or more
293-
L<adverbial pairs|/language/syntax#Adverbial_pairs_(colon_pairs)> to an ordinary
294-
identifier to create a so-called I<extended identifier>. When appended
295-
to an identifier (that is, in postfix position), the adverbial pair syntax
282+
It is often convenient to have names that contain characters that are not allowed
283+
in ordinary identifiers. Use cases include situations where a set of entities shares
284+
a common "short" name, but still needs for each of its elements to be identifiable
285+
individually. For example, you might use a module whose short name is C<Dog>, while
286+
its long name includes its naming authority and version:
287+
288+
=begin code :skip-test
289+
Dog:auth<Somebody>:ver<1.0> # Long module names including author and version
290+
Dog:auth<Somebody>:ver<2.0>
291+
292+
------------------------------------
293+
294+
use Dog:auth<Somebody>:ver<2.0>; # Selection of second module causes its full name
295+
# to be aliased to the short name for the rest of
296+
# the lexical scope, allowing a declaration like
297+
my Dog $spot .= new("woof"); # this.
298+
=end code
299+
300+
Similarly, sets of operators work together in various syntactic categories with names
301+
like C<prefix>, C<infix>, C<postfix>, etc. The long, official names of these operators,
302+
however, often contain characters that are excluded from ordinary identifiers:
303+
304+
infix:<+> # the official name of the operator in $a + $b
305+
infix:<*> # the official name of the operator in $a * $b
306+
infix:«<=» # the official name of the operator in $a <= $b
307+
308+
For all such uses, you can append one or more subscript-like adverbial forms or "colon
309+
pairs" to an ordinary identifier to create a so-called I<extended identifier>. When
310+
appended to an identifier (that is, in postfix position), the adverbial syntax
296311
generates unique variants of that identifier.
297312
298-
The generic form of an adverbial pair or "colon pair" is C<:key<value>> :
299-
it starts with a single colon C<:>, followed by an ordinary identifier C<key>,
300-
in turn followed by some quoting bracketing construct, such as C«< >», C<« »> or
301-
C<[' ']>, which quotes one or more arbitrary characters C<value>:
313+
The generic colon pair notation acceptable in identifiers is C<:key<value>> : it starts
314+
with a single colon C<:>, followed by an ordinary identifier C<key>, in turn followed
315+
by some quoting bracketing construct, such as C«< >», C<« »> or C<[' ']>, which quotes
316+
one or more arbitrary characters C<value>:
302317
303318
=begin code :skip-test
304-
# exemplary extended identifiers:
305-
infix:<+> # The official long name of the operator in $a + $b
319+
# exemplary valid extended identifiers:
306320
postfix:<²> # The official long name of the operator in $x²
321+
WOW:That'sAwesome
307322
WOW:That's<<🆒>>
308-
ThatModule:auth<Somebody>:ver<2.7.18>
309-
=end code
323+
party:sweet<16>
310324
311-
The C<key> I<or> the quoted C<value> is optional. Colon pairs with a null key
312-
C<key> are normally used for naming operators, as in the first two examples
313-
above. Starting with Perl 6 language version 6.d, colon pairs with C<sym> as
314-
the C<key> (e.g. C«:sym<foo>») are reserved for possible future use.
325+
# exemplary invalid extended identifiers:
326+
party:16<sweet> # 16 is not an ordinary identifier
327+
party:16sweet
328+
party:!a # ...and neither is !a
329+
party:$a # ...or $a
330+
=end code
331+
N<As the examples illustrate, the C<key> I<or> the quoted C<value> is optional.
332+
Colon pairs with a null key are normally used for naming operators, as
333+
in the first two examples above. Starting with Perl 6 language version 6.d, colon
334+
pairs with C<sym> as the C<key> (e.g. C«:sym<foo>») are reserved for possible
335+
future use.>
315336
316-
In an extended identifier, the adverb is considered an integral part of the name,
337+
In an extended identifier, the colon pair is considered an integral part of the name,
317338
so C<infix:<+>> and C<infix:<->> are two different operators. The bracketing
318339
characters used, however, do not count as part of the name; only the quoted data
319340
matters. So these are all the same name:
@@ -346,7 +367,7 @@ effectively name the same module:
346367
use ThatModule:auth<Somebody>:ver<2.7.18.28.18>
347368
use ThatModule:ver<2.7.18.28.18>:auth<Somebody>
348369
349-
Furthermore, the adverbial pair syntax in extended identifiers supports
370+
Furthermore, the adverbial form in extended identifiers supports
350371
compile-time interpolation, which mandates the use of
351372
L<constants|/language/terms#Constants> for the interpolation values:
352373
@@ -371,7 +392,7 @@ names.
371392
A compound identifier is an identifier that is composed of two or more
372393
ordinary and/or extended identifiers that are separated from one another by a
373394
double colon C<::>. Note that the C<::> is referred to as a I<double colon>,
374-
and not as a I<colon pair>. The latter term refers to the adverbial pair syntax
395+
and not as a I<colon pair>. The latter term refers to the adverbial syntax
375396
C<:key<value>> that may be used to create a Pair, or, as described above, to
376397
create an extended identifier.
377398

0 commit comments

Comments
 (0)