Undefined values: Difference between revisions

Line 810:
my Str $z; say $z.WHAT; # Str()</lang>
 
The user-interface for definedness are [http://design.perl6.org/S12.html#Abstract_vs_Concrete_types type smilies] and the <tt>with</tt>-statement.
 
<lang perl6>my Int:D $i = 1; # if $i has to be defined you must provide a default value
Line 820:
with 0 { say '0 may not divide but it is defined' }
</lang>
 
There are further some [http://design.perl6.org/S03.html operators] for your convenience.
 
<lang perl6>my $is-defined = 1;
my $ain't-defined = Any;
my $doesn't-matter;
my Any:D $will-be-defined = $ain't-defined // $is-defined // $doesn't-matter;
 
my @a-mixed-list = Any, 1, Any, 'a';
$will-be-defined = [//] @a-mixed-list; # [//] will return the first defined value
 
my @a = Any,Any,1,1;
my @b = 2,Any,Any,2;
my @may-contain-any = @a >>//<< @b; # contains: [2, Any, 1, 1]
 
sub f1(){Failure.new('WELP!')};
sub f2(){ $_ ~~ Failure }; # orelse will kindly set the topic for us
my $s = (f1() orelse f2()); # Please note the parentheses, which are needed because orelse is
# much looser then infix:<=> .
dd $s; # this be Bool::False</lang>
 
Finally, another major group of undefined values represents failures. Perl 6 is designed with the notion that throwing exceptions is bad for parallel processing, so exceptions are thrown lazily; that is, they tend to be returned in-band instead as data, and are only thrown for real if not properly handled as exception data. (In which case, the exception is required to report the original difficulty accurately.) In order not to disrupt control flow and the synchronicity of event ordering, such failures are returned in-band as a normal values. And in order not to subvert the type system when it comes to return types, failure may be mixed into any reference type which produces an undefined, typed value which is also indicates the nature of the failure.
Anonymous user