Skip to content

Commit ae41ef8

Browse files
committed
[operators] describe some circumfix and postcircumfixes
1 parent f308c62 commit ae41ef8

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

lib/operators.pod

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,114 @@ term or as a prefix. Subroutine calls are the most common listops. Other
101101
cases include meta-reduced infix operators (C<[+]| 1, 2, 3>) and the
102102
L<#prefix ...> etc. stub operators.
103103
104+
(Niecza currently turns postcircumfix operators in a subroutine call,
105+
while Rakudo interprets them as methods).
104106
105-
=head1 Method Postfix
107+
=head1 Term Precedence
108+
109+
=head2 circumfix < >
110+
111+
The quote-words construct. Breaks up the contents on whitespace, and returns
112+
a C<Parcel> of the words. If a word
113+
looks like a number literal or a C<Pair> literal, it is converted the
114+
appropriate number.
115+
116+
say <a b c>[1]; # b
117+
118+
(Rakudo currently always returns a parcel of strings).
119+
120+
=head2 circumfix ( )
121+
122+
The grouping operator.
123+
124+
An empty group C<()> creates an empty L<Parcel>.
125+
Parens around non-empty expressions simply structure the expression, but
126+
not have additional semantics.
127+
128+
In an argument list, putting parenthesis around an argument prevents it from
129+
being interpreted as a named argument.
130+
131+
multi sub p(:$a!) { say 'named' }
132+
multi sub p($a) { say 'positional' }
133+
p a => 1; # named
134+
p (a => 1); # positional
135+
136+
=head2 circumfix { }
137+
138+
Block or L<Hash> constructor.
139+
140+
If the contents looks like a list of pairs and does not use L<$_> or other
141+
placeholder parameters, returns an itemized L<Hash>.
142+
143+
Otherwise it contructs a L<Block>.
144+
145+
Note that this construct does not reparse the contents; rather the
146+
contents are always parsed as a statement list (i.e. like a block),
147+
and if the later analysis shows that it needs to be interpreted as a hash,
148+
the block is executed and coerced to L<Hash>.
149+
150+
=head2 circumfix [ ]
151+
152+
The L<Array> constructor. Returns an itemized L<Array> which does not flatten
153+
in list context.
154+
155+
=head1 Method Postfix Precedence
156+
157+
=head2 postcircumfix { }
158+
159+
The hash indexing postcircumfix. Fail on type L<Any>, and on L<EnumMap>,
160+
L<Hash> and related types it allows lookup of hash elements by key.
161+
162+
my %h = a => 1, b => 2;
163+
say %h{'a'}; # 1
164+
say %h{'a', 'b'}; # 1, 2
165+
166+
=head2 postcircumfix < >
167+
168+
The hash indexing quote-words operator. Interprets the argument list
169+
as a list of words, just like C<< circumfix < > >>, and then calls
170+
C<< postcircumfix:<{ }> >>, i.e. the hash indexing operator.
171+
172+
Thus you can write
173+
174+
my %h = a => 1, b => 2;
175+
say %h<a>; # 1
176+
say %h<b a>; # 2, 1
177+
178+
=head2 postcircumfix ( )
179+
180+
The call operator. Treats the invocant as a L<Callable> and invokes it,
181+
using the expression between the parens as arguments.
182+
183+
Note that an identifier followed by a pair of parens is always parsed as a
184+
subroutine call.
185+
186+
If you want your objects to respond to the call operator, you need to
187+
implement a C<< method postcircumfix:<( )> >>.
188+
189+
=head2 postcircumfix [ ]
190+
191+
The array indexing operator. Treats the invocant as a L<Positional> and
192+
indexes it by position.
193+
194+
my @a = 'a' .. 'z';
195+
say @a[0]; # a
196+
197+
Lists of indexes produce list of results as if they were all indexed
198+
separetely.
199+
200+
my @a = 'a' .. 'z';
201+
say @a[15, 4, 17, 11].join; # perl
202+
203+
L<Callable> indexes are invoked with the number of elements as arguments.
204+
205+
This lets you write
206+
207+
my @a[*-1]; # z
208+
209+
to index lists and arrays from the end.
210+
211+
Non-L<Positional> invocants are interpreted as a one-list element of the object.
106212
107213
=head2 postfix .
108214

0 commit comments

Comments
 (0)