Permalink
Browse files

Fixes errors

Basically changes say to put, which always calls Str (while say calls
.gist). Closes #2546
  • Loading branch information...
JJ committed Jan 11, 2019
1 parent 800a5b9 commit 4830298860bba31ce22e7f29fe05f48cb3c1cb57
Showing with 23 additions and 17 deletions.
  1. +23 −17 doc/Language/contexts.pod6
@@ -44,7 +44,13 @@ foo
=head1 Number X<|number context>
This context, and probably all of them except sink above, are I<conversion> or I<interpretation> contexts in the sense that they take an untyped or typed variable and duck-type it to whatever is needed to perform the operation. In some cases that will imply a conversion (from L<Str|/type/Str> to L<Numeric|/type/Numeric>, for instance); in other cases simply an interpretation (L<IntStr|/type/IntStr> will be interpreted as L<Int|/type/Int> or as L<Str|/type/Str>).
This context, and probably all of them except sink above, are I<conversion> or
I<interpretation> contexts in the sense that they take an untyped or typed
variable and duck-type it to whatever is needed to perform the operation. In
some cases that will imply a conversion (from L<Str|/type/Str> to
L<Numeric|/type/Numeric>, for instance); in other cases simply an interpretation
(L<IntStr|/type/IntStr> will be interpreted as L<Int|/type/Int> or as
L<Str|/type/Str>).
I<Number context> is called whenever we need to apply a numerical operation on a
variable.
@@ -56,7 +62,7 @@ say $not-a-string+$neither-a-string; # OUTPUT: «4␤»
=end code
In the code above, strings will be interpreted in numeric context as long as
there is only some numbers and no other characters. It can have any number of
there are only a few digits and no other characters. It can have any number of
leading or trailing whitespace, however.
Numeric context can be forced by using arithmetic operators such as C<+> or
@@ -84,16 +90,16 @@ used, for instance, for coercing non-string values so that they can be printed
to standard output.
=for code :preamble<my $very-complicated-and-hairy-object>
say $very-complicated-and-hairy-object; # OUTPUT: something meaningful
put $very-complicated-and-hairy-object; # OUTPUT: something meaningful
Or when smartmatching to a regular expression:
say 333444777 ~~ /(3+)/; # OUTPUT: «「333」␤ 0 => 「333」␤»
put 333444777 ~~ /(3+)/; # OUTPUT: «「333」␤ 0 => 「333」␤»
In general, the L<C<Str> routine|/routine/Str> will be called on a variable to
contextualize it; since this method is inherited from L<Mu|/type/Mu>, it is always
present, but it is not always guaranteed to work. In some core classes it will
issue a warning.
contextualize it; since this method is inherited from L<Mu|/type/Mu>, it is
always present, but it is not always guaranteed to work. In some core classes it
will issue a warning.
L<C<~>|/routine/~> is the (unary) string contextualizer. As an operator, it
concatenates strings, but as a prefix operator it becomes the string context
@@ -116,7 +122,7 @@ string:
say [~] [] ; # OUTPUT: «␤»
Since L<C<~> acts also as buffer concatenation operator|/routine/~#(Operators)_infix_~>,
using it will
it will
have to check that every element is not empty, since a single empty buffer in
string context will behave as a string, thus yielding an error.
@@ -130,8 +136,7 @@ my $non-empty = Buf.new(0x3, 0x33);
my $empty = [];
my $non-empty-also = Buf.new(0x2,0x22);
say [~] $non-empty, $empty, $non-empty-also;
# OUTPUT: «(exit code 1) Cannot use a Buf as a string, but you called the Stringy method on it
# in block <unit> at /tmp/bO_heb6AS9 line 1␤␤»
# OUTPUT: «Cannot use a Buf as a string, but you called the Stringy method on it
=end code
Since C<~> is putting in string context the second element of this list,
@@ -140,22 +145,23 @@ using the second form that applies to strings, thus yielding the shown error.
Simply making sure that everything you concatenate is a buffer will avoid this
problem.
my $non-empty = Buf.new(0x3, 0x33);
my $empty = Buf.new();
my $non-empty-also = Buf.new(0x2,0x22);
say [~] $non-empty, $empty, $non-empty-also; # OUTPUT: «Buf:0x<03 33 02 22>␤»
=for code
my $non-empty = Buf.new(0x3, 0x33);
my $empty = Buf.new();
my $non-empty-also = Buf.new(0x2,0x22);
say [~] $non-empty, $empty, $non-empty-also; # OUTPUT: «Buf:0x<03 33 02 22>␤»
In general, a context will coerce a variable to a particular type by calling the
contextualizer; in the case of mixins, if the context class is mixed in, it will
behave in that way.
my $described-number = 1i but 'Unity in complex plane';
say $described-number; # OUTPUT: «Unity in complex plane␤»
put $described-number; # OUTPUT: «Unity in complex plane␤»
C<but> creates a mixin, which endows the complex number with a C<Str> method.
C<say> puts into string context, that is, it calls C<Str>, the string
C<put> contextualizes it into a string, that is, it calls C<Str>, the string
contextualizer, with the result shown above.
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 comments on commit 4830298

Please sign in to comment.