Skip to content

Commit 6381a9d

Browse files
committed
Clarifies trans behavior, closes #2272
1 parent 6bb75cb commit 6381a9d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

doc/Type/Str.pod6

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ Replaces one or many characters with one or many characters. Ranges are
12051205
supported, both for keys and values. Regexes work as keys. In case a list of
12061206
keys and values is used, substrings can be replaced as well. When called with
12071207
C<:complement> anything but the matched value or range is replaced with a
1208-
single value. With C<:delete> the matched characters are without corresponding
1208+
single value; with C<:delete> the matched characters without corresponding
12091209
replacement are removed. Combining C<:complement> and C<:delete> will remove
12101210
anything but the matched values, I<unless replacement characters have been
12111211
specified>, in which case, C<:delete> would be ignored. The adverb C<:squash>
@@ -1229,6 +1229,28 @@ Example:
12291229
"aaa1123bb123c".trans('a'..'z' => 'A'..'Z', :squash); # RESULT: «A1123B123C»
12301230
"aaa1123bb123c".trans('a'..'z' => 'x', :complement, :squash); # RESULT: «aaaxbbxc»
12311231
1232+
Please note that the behavior of the two versions of the multi method is
1233+
slightly different. The first form will transpose only one character if the
1234+
origin is also one character:
1235+
1236+
=begin code
1237+
say "abcd".trans( "a" => "zz" ); # OUTPUT: «zbcd␤»
1238+
say "abcd".trans( "ab" => "yz" ); # OUTPUT: «yzcd␤»
1239+
=end code
1240+
1241+
In the second case, behavior is as expected, since the origin is more than one
1242+
char long. However, if the C<Pair> in the multi method does not have a C<Str> as
1243+
an origin or target, it is handled to the second multi method, and behavior
1244+
changes:
1245+
1246+
m: say "abcd".trans: ["a"] => ["zz"]; # OUTPUT: «zzbcd␤»
1247+
1248+
In this case, neither origin nor target in the C<Pair> are C<Str>; the method
1249+
with the C<Pair> signature then calls the second, making this call above
1250+
equivalent to C<"abcd".trans: ["a"] => ["zz"],> (with the comma behind, making
1251+
it a C<Positional>, instead of a C<Pair>), resulting in the behavior shown as
1252+
output.
1253+
12321254
=head2 method indent
12331255
12341256
multi method indent(Int $steps where { $_ == 0 } )

0 commit comments

Comments
 (0)